Skip to content

Commit

Permalink
Reconfigure example so that RestKit and MagicalRecord share managed o…
Browse files Browse the repository at this point in the history
…bject contexts instead of just the persistent store coordinator.
  • Loading branch information
blakewatters committed Jan 11, 2013
1 parent 7a83153 commit 6db44de
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions RKMagicalRecord/RKMRAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 6db44de

Please sign in to comment.