Last active
October 22, 2018 14:22
-
-
Save chourobin/4727113 to your computer and use it in GitHub Desktop.
Setting up magical record with RestKit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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
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:
As a result you can configure the MagicalRecord to use RestKit's Core Data stack as it is described above: