Skip to content

Instantly share code, notes, and snippets.

@sammcewan
Created December 30, 2012 05:02
Show Gist options
  • Save sammcewan/4411085 to your computer and use it in GitHub Desktop.
Save sammcewan/4411085 to your computer and use it in GitHub Desktop.
This Gist will intertwine RK and MagicalRecord.
// Initialize RestKit
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:httpClient];
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
// Complete Core Data stack initialization
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;
// Invoke mappings method
[self setMappings:managedObjectStore objectManager:objectManager];
NSString *databaseName = @"DBName.sqlite";
[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:databaseName];
NSError *error;
NSPersistentStore *persistentStore;
persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath
fromSeedDatabaseAtPath:nil
withConfiguration:nil
options:nil
error:&error];
[ [NSURLCache sharedURLCache] removeAllCachedResponses];
[managedObjectStore createManagedObjectContexts];
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
// Add to magical record
[NSManagedObjectContext MR_initializeDefaultContextWithCoordinator:managedObjectStore.persistentStoreCoordinator];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment