In our application we have several UITableViews which have network-backed data sources which can take time to actually populate. Here's what typically happens:
- User triggers view.
- In viewDidLoad, the view's datasource is created.
- The model's -load method is called
- The model issues a network request to get some data ...
- Before the request finishes, the user backs out of the view.
- The view's ViewWillDisappear, ViewDidDisappear, ViewDidUnload, and dealloc methods are called.
- The request finishes. Unfortunately, all or part of the table view is gone, so havoc ensues.
So, our solution has been to properly "tear down" the data source and cancel any outstanding network requests so this doesn't happen. The questions I have are:
What is the "proper" way to tear down a data source? Do you you just lose all references to it, and use the -dealloc to cancel outstanding network requests?
Where SHOULD you tear it down? In ViewDidUnload? dealloc?
The same two questions apply to the Model, actually: What's the right way to tear it down? And when?
Note that this doesn't only apply to network requests: We have another view which uses Geolocation, and sometimes by the time the Geolocation completion block is called, the view it's supposed to update is long gone.
Thanks!
P.S. Additional question:
- In the case of UITableView, is it possible the rows in the view are deallocated BEFORE the UITableView's -dealloc is called? If they are deallocated at, say, ViewWillDisappear time, and the request happens to finish after that, but BEFORE -dealloc, then if the request happens to try and update UITableView rows, havoc will ensue.
Does that make sense?