From 6db44de457353e710be3c9c30931073b9d71e3d8 Mon Sep 17 00:00:00 2001 From: Blake Watters Date: Thu, 10 Jan 2013 20:18:29 -0500 Subject: [PATCH] Reconfigure example so that RestKit and MagicalRecord share managed object contexts instead of just the persistent store coordinator. --- RKMagicalRecord/RKMRAppDelegate.m | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/RKMagicalRecord/RKMRAppDelegate.m b/RKMagicalRecord/RKMRAppDelegate.m index a249db9..4a11379 100644 --- a/RKMagicalRecord/RKMRAppDelegate.m +++ b/RKMagicalRecord/RKMRAppDelegate.m @@ -11,20 +11,31 @@ #import "RKMRAppDelegate.h" #import "RKMRMasterViewController.h" +// Use a class extension to expose access to MagicalRecord's private setter methods +@interface NSManagedObjectContext () ++ (void)MR_setRootSavingContext:(NSManagedObjectContext *)context; ++ (void)MR_setDefaultContext:(NSManagedObjectContext *)moc; +@end + @implementation RKMRAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - // Setup MagicalRecord - [MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:@"RKMagicalRecord.sqlite"]; - - /** - Configure RestKit to share a Persistent Store Coordinator with MagicalRecord. This ensures that object request operations will persist back to the same Persistent Store managed by MagicalRecord, making managed objects available across the libraries. - */ - NSPersistentStoreCoordinator *persistentStoreCoordinator = [NSPersistentStoreCoordinator MR_defaultStoreCoordinator]; - RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithPersistentStoreCoordinator:persistentStoreCoordinator]; + // Configure RestKit's Core Data stack + NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"RKMagicalRecord" ofType:@"momd"]]; + NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy]; + RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel]; + NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"RKMagicalRecord.sqlite"]; + NSError *error = nil; + [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error]; [managedObjectStore createManagedObjectContexts]; + // Configure MagicalRecord to use RestKit's Core Data stack + [NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:managedObjectStore.persistentStoreCoordinator]; + [NSManagedObjectContext MR_setRootSavingContext:managedObjectStore.persistentStoreManagedObjectContext]; + [NSManagedObjectContext MR_setDefaultContext:managedObjectStore.mainQueueManagedObjectContext]; + + RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://restkit.org"]]; objectManager.managedObjectStore = managedObjectStore;