Skip to content

Instantly share code, notes, and snippets.

@tjmehta
Created September 7, 2012 21:14
Show Gist options
  • Select an option

  • Save tjmehta/3669711 to your computer and use it in GitHub Desktop.

Select an option

Save tjmehta/3669711 to your computer and use it in GitHub Desktop.
//
// in AppDelegate
//
// Initialize RestKit Shared Client
RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURLString:_baseURL];
// Enable automatic network activity indicator management
objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
// Initialize object store
// NSString *seedDatabaseName = RKDefaultSeedDatabaseFileName;
NSString *databaseName = @"PSDatabase.sqlite";
RKManagedObjectStore *objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:databaseName];
// objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:databaseName usingSeedDatabaseName:seedDatabaseName managedObjectModel:nil delegate:self];
/*!
Map to a target object class -- just as you would for a non-persistent class. The entity is resolved
for you using the Active Record pattern where the class name corresponds to the entity name within Core Data.
*/
// User Mapping
RKManagedObjectMapping *userMapping = [RKManagedObjectMapping mappingForClass:[User class] inManagedObjectStore:objectStore];
userMapping.primaryKeyAttribute = @"id";
[userMapping mapKeyPath:@"_id" toAttribute:@"id"];
[userMapping mapAttributes:@"name", @"email", @"token", @"phoneNumber", @"emailVerified", nil];
[userMapping mapKeyPath:@"accounts.facebook.userId" toAttribute:@"facebookId"];
[userMapping mapKeyPath:@"accounts.facebook.token" toAttribute:@"facebookToken"];
[userMapping mapKeyPath:@"accounts.twitter.userId" toAttribute:@"twitterId"];
[userMapping mapKeyPath:@"accounts.twitter.token" toAttribute:@"twitterToken"];
[objectManager.mappingProvider setMapping:userMapping forKeyPath:@"users"];
[objectManager.mappingProvider setMapping:userMapping forKeyPath:@"user"];
[RKObjectManager sharedManager].serializationMIMEType = RKMIMETypeJSON;
RKObjectMapping *userSerializationMapping = [userMapping inverseMapping];
//userSerializationMapping - buildPathIfDoesNotExist
[objectManager.mappingProvider setSerializationMapping:userSerializationMapping forClass:[User class]];
[objectManager.router routeClass:[NSMutableDictionary class] toResourcePath:@"/users"];
[objectManager.router routeClass:[User class] toResourcePath:@"/users"];
[RKObjectManager setSharedManager:objectManager];
// in ViewController
// in ViewController
// in ViewController
User *facebookUser = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:self.psDatabase.managedObjectContext.parentContext]; //parent context moves it to diff thread.
facebookUser.facebookToken = FBSession.activeSession.accessToken;
[[RKObjectManager sharedManager] postObject:facebookUser usingBlock:^(RKObjectLoader *userLoader){
NSLog(@"%@", userLoader);
userLoader.onDidFailWithError = ^(NSError *error){
//TODO: error modal here, logout of facebook.
NSLog(@"%@", @"userloader failed.");
};
userLoader.onDidLoadObject = ^(id object){
//facebookUser =
if (true) {
NSLog(@"yes");
// [self performSegueWithIdentifier:@"UserAuthenticated" sender:self];
}
else {
//logout facebook
NSLog(@"no");
}
};
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment