Question

This is just a theoretical question. It was born from a real problem in my app, but I re-designed the problem out of the application. But the question remains:

If in my app delegate I write my singleton object to disk upon applicationWillTerminate: but also use NSNotificationCenter to call updateSingletonData upon UIApplicationWillTerminateNotification in some view controller, which will happen first? Will my data be written to the singleton, then the singleton be written to disk, then the app terminates? Or will the reverse happen, with the singleton being serialized and then the singleton updated (worse), or will the app just terminate after a certain amount of time if the serialization takes too long (much worse!)?

I guess this shows my lack of understanding of the guts of Springboard... thanks to anyone who can shed some light here.

Was it helpful?

Solution

A couple of things to note here:

  1. Only Apple know the order these will happen in, as they wrote the code that does it.

  2. You shouldn't care about the order these will happen in. If you do care, then you've designed your code badly.

In reality, you could go and check what order the happen in - for your particular device, for your particular iOS version, etc.

But really, you shouldn't care what order they happen in. From the sounds of it, you should be either firing off to the view controller to write the data before saving in applicationWillTerminate:, or letting the view controller handle saving after it's written its data.

OTHER TIPS

This question is old and the response by @mattjgalloway is correct in terms of code quality but, for the sake of the knowledge, I just saw in the docs that the notification is posted after the UIApplicationDelegate method is called (emphasis mine):

After calling this method, the app also posts a UIApplication​Will​Terminate notification to give interested objects a chance to respond to the transition.

https://developer.apple.com/reference/uikit/uiapplicationdelegate/1623111-applicationwillterminate

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top