Created
October 16, 2012 14:12
-
-
Save romanr/3899504 to your computer and use it in GitHub Desktop.
Setup RestKit Stack 0.20
This file contains hidden or 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
//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