I have a controller class that uses asynchronous calls to perform some network operations. My view hierarchy is navigation based e.g. View1 > View2 > View3.
Now lets suppose my View3 (View3Controller) uses asynchronous calls to perform some network operations, and View2Controller also implements some of the delegate methods as well.
User can use “Back” button to navigate back at anytime, and consequently [View3Controller dealloc] gets called. Now i want the async operation to complete! Is there a (safe) way to make sure that View3Controller doesn’t get dealloc‘ed until the async operation is completed?
One solution is to move the asynchronous call to its own class, preferably owned by
View2Controlleror someone higher up. When pushingView3Controlleryou just pass along this new class and if the user navigates back it will continue as you want.Imagine an object like:
and when pushing
View3Controllerand when starting the async operation from within
View3Controllerif the user navigates back you could potentially do something like this in
navigationController:willShowViewController:animated:inView2Controllerdo get the callbacks fromasyncObjectin caseView3Controllergets popped.If you need to be able to receive events from the
asyncObjectfor several objects at the same time you should look at notifications or KVO (Key Value Observing) instead of using a delegate.