I’m copying a database I include in my bundle with my app and transferring it to the Documents directory to make it writeable. It takes some time and I we currently have just a splash screen. Is there a way to put a “Loading…” alert or some sort of activity indicator (Like how Words With Friends does it when loading your games), to give the user some context as to what’s happening for the first 20 seconds the first time they open the app? I didn’t know if I had a reference to that screen somehow since the first viewController that gets presented from the AppDelegate is not loaded yet. Thanks.
Share
When performing long running tasks on application launch there are a few considerations.
Application Did Finish Launching
You must finish the
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptionswithin a certain amount of time, which I believe is 20 seconds, otherwise your application will get shut down from the watchdogNote On WatchDog Crashes
What I usually do is create a LoaderViewController and add it as a subview of the window. I still set the root view controller of the application to the first view controller though. this essentially hands off loading control to the loader view controller to perform your functions.
Although this is sometime frowned upon by Apple’s Human Interface Guidelines
iOS Human Interface Guidelines
Non Blocking
You’re also going to want to perform all your copying in the background using GrandCentral Dispatch or use
- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)argto prevent blocking the main thread. This way you can present aUIActivityIndicatorViewthat remains spinning while the task is being performed.Grand Central Dispatch
Grand Central Dispatch is probably the best way to handle this. You can perform your task in the background and update the main thread, like a UIProgressView in the same block like this
Once finished, remove your LoaderViewController
[loaderViewController removeFromSuperview];and your application will go to the first controller. At this time you can also refresh the UI to display updated information.Grand Central Dispatch Reference