I am updating a rather straightforward iPhone app that requires an initial data load into a SQLite databbase upon initial setup. I am using the DSActivityView class to provide a nice “Please wait” message. The entire data load takes about 15 seconds on WiFi and about 30-45 seconds on 3G.
This whole process originates in the application:didFinishLaunchingWithOptions method of my app delegate.
The data load procedures are being launched in a separate thread by creating an instance of an NSOperation object called UpdateTable for each of the 8 tables. Each operation is placed in an NSOperationQueue, and then released. The data loading works like a champ.
What is NOT working like a champ; however, is the replacement of the “Please wait..” view with the main navigation controller view.
The first approach was to instantiate an NSInvocationOperation that called a method in my app delegate that to execute the following:
- (void) loadNavController;
{
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
[defaultImageView removeFromSuperview];
[DSBezelActivityView removeViewAnimated:YES];
}
This operation was added to the queue after the last UpdateTable operation was added.
The reason this approach is buggy is because the NSInvocationOperation runs concurrent with the other processes in the thread; therefore, the method shown above fires before the last table update can be performed.
So I tried the following approach:
In my UpdateTable.m (where all of the JSON and SQLite is taking place), I entered the following line to execute immediately after the last table update completed:
[appDelegate performSelectorOnMainThread:@selector(loadNavController) withObject:nil waitUntilDone:NO];
This approach got the timing right, however, the main UNavigationController and UIWindow objects were both nil with loadNavController executed. Setting some debug breaks, I noticed that the delloc method of my app delegate was firing prior to the loadNavController method was executed. Both objects are assigned to the MainWindow via IBOutlets that are set up as IBOutlets (nonatomic, retain) in the App Delegate. I haven’t a clue as to why the dealloc is firing, as this process is taking less than 10 seconds in total.
Any assistance you might render would be greatly appreciated. Many thanks in advance.
VB
A quick note to update this thread with the solution.
The performSelectorOnMainThread approach turned out to be correct. The reason the navController object was nil when the selector was called was due to me creating a new instance of the app delegate:
instead of referencing the existing object:
Works like a charm.