ios - Tearing down UITableViewDataSource -
in our application have several uitableviews have network-backed data sources can take time populate. here's typically happens:
- user triggers view.
- in viewdidload, view's datasource created.
- the model's -load method called
- the model issues network request data ...
- before request finishes, user backs out of view.
- the view's viewwilldisappear, viewdiddisappear, viewdidunload, , dealloc methods called.
- the request finishes. unfortunately, or part of table view gone, havoc ensues.
so, our solution has been "tear down" data source , cancel outstanding network requests doesn't happen. questions have are:
what "proper" way tear down data source? you lose references it, , use -dealloc cancel outstanding network requests?
where should tear down? in viewdidunload? dealloc?
the same 2 questions apply model, actually: what's right way tear down? , when?
note doesn't apply network requests: have view uses geolocation, , time geolocation completion block called, view it's supposed update long gone.
thanks!
p.s. additional question:
- in case of uitableview, possible rows in view deallocated before uitableview's -dealloc called? if deallocated at, say, viewwilldisappear time, , request happens finish after that, before -dealloc, if request happens try , update uitableview rows, havoc ensue.
does make sense?
are using objective-c, c++, or objective-c++? ask because referring methods c++ syntax (::) instead of obj-c (-), , not c++ side.
usually implement -dealloc
method if object has work when deallocated (torn down). in there cancel network requests , release low-level resources file handles. method called automatically obj-c runtime when there no more references instance , ready deallocate - should not call method manually.
the accepted pattern nil out strong references object when you're done , let runtime handle deallocating it. if follow pattern everywhere (networking, file system access, gps, etc) it's pretty easy work with.
Comments
Post a Comment