Skip to content

Instantly share code, notes, and snippets.

@MPiccinato
Created January 15, 2013 21:48
Show Gist options
  • Save MPiccinato/4542422 to your computer and use it in GitHub Desktop.
Save MPiccinato/4542422 to your computer and use it in GitHub Desktop.
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:@{
@"email": @"email",
@"username": @"username",
@"password": @"password",
}];
endPoint = kUTAPIEndPointAuthenticate;
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[User class] rootKeyPath:@"User"];
// Serialize
NSError* error;
NSDictionary *parameters = [RKObjectParameterization parametersWithObject:user requestDescriptor:requestDescriptor error:&error];
NSData *serializedUser = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:RKMIMETypeFormURLEncoded error:&error];
// Request
NSMutableURLRequest *request = [[RKObjectManager sharedManager] requestWithObject:user method:RKRequestMethodPOST path:endPoint parameters:nil];
[request setValue:RKMIMETypeFormURLEncoded forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:serializedUser];
// Succes Failure
RKManagedObjectRequestOperation *operation = [[RKObjectManager sharedManager] managedObjectRequestOperationWithRequest:request
managedObjectContext:user.managedObjectContext
success:^ (RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
[self requestDidComplete:operation.HTTPRequestOperation.request];
success(mappingResult, nil);
} failure:^ (RKObjectRequestOperation *operation, NSError *error) {
[self requestDidFail:operation.HTTPRequestOperation.request error:error];
success(nil, error);
}];
// Go!
[[RKObjectManager sharedManager] enqueueObjectRequestOperation:operation];
@MPiccinato
Copy link
Author

Discovered two things for this to work correctly.

  1. Must obtain a permanent ID

    // Permanent ID
    __block BOOL _blockSuccess;
    __block NSError *_blockError;

    [[user managedObjectContext] performBlockAndWait:^{
    _blockSuccess = [[user managedObjectContext] obtainPermanentIDsForObjects:@[user] error:&_blockError];
    }];

  2. Must set the target object of the operation
    operation.targetObject = user;

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