Skip to content

Instantly share code, notes, and snippets.

@chourobin
Last active October 22, 2018 14:22
Show Gist options
  • Save chourobin/4727113 to your computer and use it in GitHub Desktop.
Save chourobin/4727113 to your computer and use it in GitHub Desktop.
Setting up magical record with RestKit
#import <RestKit/RestKit.h>
#import "CoreData+MagicalRecord.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 AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Configure RestKit's Core Data stack
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"YourApp" ofType:@"momd"]];
NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"YourApp.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;
// Pass the MagicalRecord
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
RKMRMasterViewController *controller = (RKMRMasterViewController *)navigationController.topViewController;
controller.managedObjectContext = [NSManagedObjectContext MR_defaultContext];
return YES;
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Saves changes in the application's managed object context before the application terminates.
[MagicalRecord cleanUp];
}
@end
@khawars
Copy link

khawars commented Feb 10, 2016

Any tips for Swift based project?

@balazsnemeth
Copy link

I've created an objective-c category file called "NSMangedObjectContext_MagicalRecordify.h", added to the Bridging header:

#import "NSMangedObjectContext_MagicalRecordify.h"

and the content of the file:

@interface NSManagedObjectContext (MagicalRecordify)
+ (void)MR_setRootSavingContext:(NSManagedObjectContext *)context;
+ (void)MR_setDefaultContext:(NSManagedObjectContext *)moc;
@end

As a result you can configure the MagicalRecord to use RestKit's Core Data stack as it is described above:

NSPersistentStoreCoordinator.MR_setDefaultStoreCoordinator(persistentStore)
NSManagedObjectContext.MR_setRootSavingContext(managedObjectStore.persistentStoreManagedObjectContext)
NSManagedObjectContext.MR_setDefaultContext(managedObjectStore.mainQueueManagedObjectContext) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment