Skip to content

Instantly share code, notes, and snippets.

@comiclandapp
Forked from chourobin/RKAppDelegate.m
Created October 22, 2018 14:22
Show Gist options
  • Save comiclandapp/3775e1661d53170592f7889ef96a6f10 to your computer and use it in GitHub Desktop.
Save comiclandapp/3775e1661d53170592f7889ef96a6f10 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment