Skip to content

Instantly share code, notes, and snippets.

@ryatkins
Created July 30, 2012 19:55
Show Gist options
  • Save ryatkins/3209606 to your computer and use it in GitHub Desktop.
Save ryatkins/3209606 to your computer and use it in GitHub Desktop.
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURLString:@"http://mysite.com/"];
// Core Data store
RKManagedObjectStore* objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"RKData.sqlite"];
objectManager.objectStore = objectStore;
// Activity indicator
objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
// MAPPING - Model (detail item)
RKManagedObjectMapping* modelMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Model" inManagedObjectStore:objectManager.objectStore];
modelMapping.primaryKeyAttribute = @"modelID";
[modelMapping mapKeyPath:@"id" toAttribute:@"modelID"];
[modelMapping mapKeyPath:@"title" toAttribute:@"title"];
[modelMapping mapKeyPath:@"image" toAttribute:@"image"];
[objectManager.mappingProvider registerMapping:modelMapping withRootKeyPath:@"model"];
[objectManager.mappingProvider setObjectMapping:modelMapping forResourcePathPattern:@"json.php" withFetchRequestBlock:^NSFetchRequest *(NSString *resourcePath){
return [Model fetchRequest];
}];
// MAPPING - Category (Master)
RKManagedObjectMapping* categoryMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Category" inManagedObjectStore:objectManager.objectStore];
categoryMapping.primaryKeyAttribute = @"categoryID";
[categoryMapping mapKeyPath:@"id" toAttribute:@"categoryID"];
[categoryMapping mapKeyPath:@"title" toAttribute:@"title"];
[categoryMapping mapKeyPath:@"image" toAttribute:@"image"];
[categoryMapping mapRelationship:@"models" withMapping:modelMapping];
[objectManager.mappingProvider registerMapping:categoryMapping withRootKeyPath:@"category"];
[objectManager.mappingProvider setObjectMapping:categoryMapping forResourcePathPattern:@"json.php" withFetchRequestBlock:^NSFetchRequest *(NSString *resourcePath){
return [Category fetchRequest];
}];
[{"category":
{"id":2,"title":"Your Models",
"models":[
{"id":23,"title":"Cowboy","image":"Cowboy.png"},
{"id":24,"title":"Lizard","image":"Lizard.png"}
]
}
}]
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadDetailItem];
}
- (void)loadDetailItem {
RKObjectManager* objectManager = [RKObjectManager sharedManager];
[objectManager loadObjectsAtResourcePath:@"json.php" delegate:self];
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects {
NSFetchRequest* fetchRequest = [Category fetchRequest];
self.categories = [[Category objectsWithFetchRequest:fetchRequest] retain] ;
[self.tableView reloadData];
}
@seigel
Copy link

seigel commented Aug 16, 2012

Did you get it working?

@ryatkins
Copy link
Author

No, not working yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment