Skip to content

Instantly share code, notes, and snippets.

@shyam-habarakada
Created August 29, 2011 18:17
Show Gist options
  • Save shyam-habarakada/1179001 to your computer and use it in GitHub Desktop.
Save shyam-habarakada/1179001 to your computer and use it in GitHub Desktop.
RestKit initialization
- (void)initResKitWithCoreData
{
#if DEBUG
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
#endif
[RKRequestQueue sharedQueue].showsNetworkActivityIndicatorWhenBusy = YES;
// Set the default refresh rate to 1, so we always hit the web when we can.
// If the server is unavailable, we will load from the Core Data cache.
// TODO Figure out how to properly use the RKRequestTTModel . See
// http://groups.google.com/group/restkit/browse_thread/thread/e97c202cf7392d7b/c7277322e8f07cce?lnk=gst&q=could+not+locate+an+NSManagedObjectModel+for+entity+name#c7277322e8f07cce
// [RKRequestTTModel setDefaultRefreshRate:1];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithBaseURL:[@"http://127.0.0.1:3000" retain]];
//[RKObjectManager sharedManager].serializationMIMEType = RKMIMETypeJSON;
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"APXData" withExtension:@"momd"];
NSManagedObjectModel* managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
NSString *storeDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
RKManagedObjectStore* objectStore = [[RKManagedObjectStore
objectStoreWithStoreFilename:@"APXData.sqlite"
inDirectory:storeDirectory
usingSeedDatabaseName:nil
managedObjectModel:managedObjectModel
delegate:self] autorelease];
objectManager.objectStore = objectStore;
//[[RKParserRegistry sharedRegistry] setParserClass:[SBJsonParser class] forMIMEType:RKMIMETypeJSON];
// RestKit BUG -- Need to alwyas use the sharedManager or NSManagedObject+ActiveRecord methods will throw.
[RKObjectManager setSharedManager:objectManager];
RKManagedObjectMapping *patientMapping = [RKManagedObjectMapping mappingForClass:[Patient class]];
patientMapping.primaryKeyAttribute = @"id";
[patientMapping mapKeyPath:@"id" toAttribute:@"id"];
[patientMapping mapKeyPath:@"first_name" toAttribute:@"firstname"];
[patientMapping mapKeyPath:@"last_name" toAttribute:@"lastname"];
[objectManager.mappingProvider setMapping:patientMapping forKeyPath:@"patient"];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment