Skip to content

Instantly share code, notes, and snippets.

@danielbehrendt
Forked from romanr/gist:3899504
Created January 14, 2013 10:45
Show Gist options
  • Save danielbehrendt/4529227 to your computer and use it in GitHub Desktop.
Save danielbehrendt/4529227 to your computer and use it in GitHub Desktop.
//Create store and moc:
NSURL *baseURL = [NSURL URLWithString:myUrl];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
//not sure what this does but it's in example project
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;
NSString *modelPath = nil;
for (NSBundle* bundle in [NSBundle allBundles])
{
modelPath = [bundle pathForResource:@"MyApp" ofType:@"momd"];
if (modelPath)
break;
}
NSAssert(modelPath != nil, @"Could not find managed object model.");
mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];
[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"MyApp.sqlite"];
NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"RKSeedDatabase" ofType:@"sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);
// Create the managed object contexts
[managedObjectStore createManagedObjectContexts];
// Configure a managed object cache to ensure we do not create duplicate objects
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext];
//Create mapping
RKEntityMapping * someMapping = [RKEntityMapping mappingForEntityForName:@"SomeClass" inManagedObjectStore:managedObjectStore];
[someMapping addAttributeMappingsFromDictionary:@{
@"some_key" : @"someAttr"
];
//relationship mapping
RKRelationshipMapping* relationShipMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"child"
toKeyPath:@"child"
withMapping:childeMapping];
[someMapping addPropertyMapping:relationShipMapping];
//To load objects use:
[[RKObjectManager sharedManager] getObject:[[MyObject class] path:resourcePath parameters:nil success: error:];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment