Skip to content

Instantly share code, notes, and snippets.

@chbeer
Last active December 29, 2015 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chbeer/7694490 to your computer and use it in GitHub Desktop.
Save chbeer/7694490 to your computer and use it in GitHub Desktop.
#pragma mark -
#pragma mark Core Data stack
/**
Returns the managed object context for the application.
If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
*/
- (NSManagedObjectContext *)managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [NSManagedObjectContext new];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
}
CBLIncrementalStore *store = (CBLIncrementalStore*)[coordinator persistentStores][0];
[store addObservingManagedObjectContext:managedObjectContext];
return managedObjectContext;
}
/**
Returns the managed object model for the application.
If the model doesn't already exist, it is created by merging all of the models found in the application bundle.
*/
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
[CBLIncrementalStore updateManagedObjectModel:managedObjectModel];
return managedObjectModel;
}
/**
Returns the persistent store coordinator for the application.
If the coordinator doesn't already exist, it is created and the application's store added to it.
*/
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
// NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"Recipes.sqlite"];
// /*
// Set up the store.
// For the sake of illustration, provide a pre-populated default store.
// */
// NSFileManager *fileManager = [NSFileManager defaultManager];
// // If the expected store doesn't exist, copy the default store.
// if (![fileManager fileExistsAtPath:storePath]) {
// NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Recipes" ofType:@"sqlite"];
// if (defaultStorePath) {
// [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
// }
// }
NSURL *storeUrl = [NSURL URLWithString:@"recipes"];
NSError *error;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
CBLIncrementalStore *store = (CBLIncrementalStore*)[persistentStoreCoordinator addPersistentStoreWithType:[CBLIncrementalStore type]
configuration:nil URL:storeUrl options:nil error:&error];
if (!store) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
Typical reasons for an error here include:
* The persistent store is not accessible
* The schema for the persistent store is incompatible with current managed object model
Check the error message to determine what the actual problem was.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[store replicateWithURL:[NSURL URLWithString:@"http://127.0.0.1:5984/recipes"] exclusively:YES];
return persistentStoreCoordinator;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment