iOS: How safe is serializing objects for data persistence on applicationWillTerminate? Any chance that the serialization will not complete?
I understand that you generally serialize objects for data persistence using the NSCoder protocol on applicationWillTerminate. Is there any good chance that this serialization will not complete though? I.e. the battery is about to die, so the app quits, and before the serialization can complete, the battery totally runs out.
I'm creating an app where the process for the user to add a new object (like a financial record) is pretty involved. Therefore, a failure to serialize the object would be pretty annoying because the recreation of that object would be a bit time consuming. Should I therefore do the serialization immediately after the user clicks the "Save" button, or can I be certain that the serialization will complete 99.9% of the time on applicationWillTerminate?
On a similar note, if I am having this dat开发者_运维百科a synced to a server, will my app have enough time to do this syncing on applicationDidEnterBackground?
For applications that do not opt out of multitasking (by setting UIApplicationExitsOnSuspend
to YES
in the Info.plist), applicationWillTerminate:
will not always be called, you should do your serialization in applicationDidEnterBackground:
instead.
The most significant risk is that your serialization takes too long and the watchdog daemon kills your app before it finishes. This will happen if your serialization is taking more than about 5 seconds, which could very well happen if you serialize large objects.
精彩评论