Looking at Apple documentation, I see that they are recommending removing Core Data initialization code out of AppDelegate. Their approach is below.
What I don't understand are the following
- The sentence below in the documentation. How is there a callback to app delegate? I don't see one in the code snippets below. Is it something they want us to add.
By initializing a separate controller object with a completion block, you have moved the Core Data stack out of the application delegate, but you still allow a callback to the application delegate so that the user interface can know when to begin requesting data.
- AppDelegate calls init of DataController and this in turn this calls initializeCoreData. But initializeCoreData sets up the persistent store co-ordinator in a background thread. Which means that if we transition to the application's first view and its view controller requests data from core-data, things are not yet set up. Won't this be an issue? Does this mean they want us to show a different launch screen & register for a callback that tells us that CoreData initialization is done before moving to actual first application view.
AppDelegate code in documentation
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setDataController:[[DataController alloc] init];
// Basic User Interface initialization
return YES;
}
DataController code in documentation
@interface MyDataController : NSObject
@property (strong) NSManagedObjectContext *managedObjectContext;
-(void)initializeCoreData;
@end
@implementation MyDataController
-(id)init {
self = [super init];
if (!self) return nil;
[self initializeCoreData];
return self;
}
- (void)initializeCoreData {
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"DataModel" withExtension:@"momd"];
NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
NSAssert(mom != nil, @"Error initializing Managed Object Model");
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[moc setPersistentStoreCoordinator:psc];
[self setManagedObjectContext:moc];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentsURL = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *storeURL = [documentsURL URLByAppendingPathComponent:@"DataModel.sqlite"];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
NSError *error = nil;
NSPersistentStoreCoordinator *psc = [[self managedObjectContext] persistentStoreCoordinator];
NSPersistentStore *store = [psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error];
NSAssert(store != nil, @"Error initializing PSC: %@\n%@", [error localizedDescription], [error userInfo]);
});
}