Is applicationDidEnterBackground ALWAYS called before applying WillTerminate?

Is applicationDidEnterBackground ALWAYS called before applicationWillTerminate in an iOS app? I know that applicationWillTerminate not always called (multitasking) - but when it is called, is applicationDidEnterBackground ALWAYS called first? I don't want to duplicate code unnecessarily by including it in applicationWillTerminate if it is already included in applicationDidEnterBackground , for an application that supports multitasking.

+7
ios order lifecycle multitasking
Dec 05
source share
1 answer

in ios 4.0 and later, applicationDidEnterBackground is called instead of applicationWillTerminate , so you do not need to call both of them. Here are some of Apple's docs:

Discussion

In iOS 4.0 and later, this method is called instead of the applicationWillTerminate: method when a user closes an application that supports background execution. You must use this method to free up shared resources, save user data, cancel timers, and store enough information about the state of the application to restore your application to its current state if it is terminated later. You should also disable the UI update of your application and not use some types of system resources (for example, user contacts database). It is also imperative to avoid using OpenGL ES in the background.

Your implementation of this method has approximately five seconds to complete any tasks and return. If you need extra time to complete any final tasks, you can request additional execution time from the call to beginBackgroundTaskWithExpirationHandler :. In practice, you should return with applicationDidEnterBackground: as quickly as possible. If the method does not return before the time runs out, your application terminates and is cleared from memory.

You must complete any tasks related to setting up the user interface before this method exits, but other tasks (for example, saving state) should, if necessary, be moved to a parallel send queue or a secondary thread. Since, probably, any background tasks that you start applicationDidEnterBackground: will not work until this method exits, you should request additional background execution time before starting these tasks. In other words, the first call to beginBackgroundTaskWithExpirationHandler: and then run the task on the send queue or secondary thread.

The app also publishes a UIApplicationDidEnterBackgroundNotification Notification around at the same time it calls this method to give interested objects a chance to respond to the transition.

For more information on how to correctly switch to the background, as well as information on how to start background tasks during exit, see the iOS Application Programming Guide.

Hope this helps solve the problem for you. Adrian

Here is a link to the technical note, which is available in the developer section. He deals with networking and multitasking. The actual method used in this document deals only with applicationDidEnterBackground , and with iOS 5 they have a system called a watchdog timer that terminates the application if the network does not respond automatically. Therefore, there is no need to actually call applicationWillTerminate and try to execute the codes so that your application can complete its task before the application is completed. The application will enter the background mode and continue its task until the completion of the last task. Hope this makes sense, but here is the link. Please read the watchdog section. https://developer.apple.com/library/ios/#technotes/tn2277/_index.html#//apple_ref/doc/uid/DTS40010841

Hope this helps. :)

+5
Dec 05 '12 at 16:45
source share



All Articles