Search code examples
iosobjective-cafnetworkingafnetworking-2

Check download from other view controller AFNetworking iOS


I have a ViewController with a UITableView inside of it, which lists files that are on a server. Each Cell of this Table has some indicators that shows the progress of the download and other stuff.

The problem:

When I go back to other ViewController and come back to the controller with the TableView, the progress and indicators in the cell appear like if the file isn't downloading, but the file is still downloading in background.

My possible solution:

Having a global NSMutableArray which contains all the downloads in several NSDictionary objects (each dictionary is a download, it has the progress, index of the row).

If I exit my ViewController with the Table and come back, I check if there are any downloads in progress (by counting the objects in the Downloads Array) and start a timer which will update each of the items in the Downloads Array and pushing the changes to the TableView.

(I use AFURLConnectionOperation, with progressBlock for updating the indicators)

Is there a better solution? Thanks!


Solution

  • Whether your mutable array is a simple global or some singleton or a property of the app delegate or of root view controller is up to you, but the idea is right, to pull the coordinating of downloads out of the presented view controller, itself.

    The thing that doesn't seem right, though, is the use of the timer. Rather than a timer which is polling for download progress and updating table accordingly, I'd advise some notification-based approach whereby downloader post notifications to [NSNotificationCenter defaultCenter] as downloads progress/complete and then have the view controller register itself as an observer for these notifications, updating the UI as notifications come in.