Created
January 15, 2013 21:48
-
-
Save MPiccinato/4542422 to your computer and use it in GitHub Desktop.
This file contains 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
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]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Discovered two things for this to work correctly.
Must obtain a permanent ID
// Permanent ID
__block BOOL _blockSuccess;
__block NSError *_blockError;
[[user managedObjectContext] performBlockAndWait:^{
_blockSuccess = [[user managedObjectContext] obtainPermanentIDsForObjects:@[user] error:&_blockError];
}];
Must set the target object of the operation
operation.targetObject = user;