Reputation: 137
I have an app that creates a persistent store in application:didFinishLaunchingWithOptions. Adding the store can apparently take too long, which causes iOS to terminate the app before it finishes launching. How to add the store to the coordinator off the main thread? Any examples? stackoverflow link
OS Version: iOS 6.1.3 (10B329) Report Version: 104
Exception Type: 00000020 Exception Codes: 0x000000008badf00d Highlighted Thread: 0
Application Specific Information: com.maksanzhi.person failed to launch in time
Elapsed total CPU time (seconds): 13.470 (user 13.470, system 0.000), 22% CPU Elapsed application CPU time (seconds): 0.343, 1% CPU
Thread 0 name: Dispatch queue: com.apple.main-thread Thread 0:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"COOLPERSONS.sqldata"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:[storeURL path]]) {
NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"COOLPERSONS" withExtension:@"sqldata"];
if (defaultStoreURL) {
[fileManager copyItemAtURL:defaultStoreURL toURL:storeURL error:NULL];
[self addSkipBackupAttributeToItemAtURL:storeURL];
}
}
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
MainViewController *cvtc = (MainViewController *)[[nav viewControllers]objectAtIndex:0];
cvtc.managedObjectContext = self.managedObjectContext;
[self setAppereance];
return YES;
}
Upvotes: 2
Views: 721
Reputation: 26385
as general advice you should not create nothing heavy in the applicationDidFinishLaunching method. The Core data Persistent Store isn't heavy to create, but in the feature could be, due to migration process. To be sure that is the persistent store and not something else, try to comment out that part of code. Are you initializing the store with data?
Upvotes: 3