I’m currently kicking off a background thread to do some REST queries in my app delegate’s didFinishLaunchingWithOptions. This thread creates some objects and populates the model as the rest of the app continues to load (because I don’t block, and didFinishLaunchingWithOptions returns YES). I also put up a loading UIViewController ‘on top’ of the main view that I tear down after the background initialization is complete.
My problem is that I need to notify the first view (call it the Home view) that the model is ready, and that it should populate itself. The trick is that the background download could have finished before Home.viewDidAppear is called, or any of the other Home.initX methods.
I’m having difficulty synchronizing all of this and I’ve thought about it long enough that it feels like I’m barking up the wrong tree.
Are there any patterns here for this sort of thing? I’m sure other apps start by performing lengthy operations with loading screens 🙂
Thanks!
I usually have a Factory class that’s responsible for wiring all the objects together. The Factory is created by the application delegate in the
applicationDidFinishLaunching:Now if the Factory creation takes long, I simply wait under the splash screen or put up another view that displays spinner until everything is ready. I guess you could use this very scheme if you can perform the initialization synchronously:
With a bit of luck there’s just a single long operation in your startup routine, so that you won’t lose anything by serializing the init.
If you want to perform the data source init in background, you can display some introductory screen that will cue the home screen once the data has been loaded:
Now when the data source has finished loading the data, it will tell the intro screen to cue the real content (home screen, in this case). This is just a rough sketch (for example the memory management might be different in the real case), but in principle it should work fine.