ZeNewb
ZeNewb

Reputation: 435

RestKit mapping result populated, but not setting Core Data entity

I am trying to do a multi-part POST to create a new user w/ an attached image. The RKMappingResult shows that it mapped the response successfully, but for some reason it's not mapping to the User entity object correctly since all of those attributes are still nil after the mapping.

RKMappingResult:

(lldb) po mappingResult
<RKMappingResult: 0xc8d04a0, results={
    user = "<User: 0xc8d4360> (entity: User; id: 0xc676560 <x-coredata://68DA437E-D836-48F7-B4CE-CC08EC38B98F/User/p15> ; data: {\n    accessToken = c81453227a39e16e396218d3a1ef0d80;\n    actions = nil;\n    createdAt = \"2013-12-11 15:34:45 +0000\";\n  email = \"user@users.com\";\n    firstName = Some;\n    fullName = \"Some User\";\n    lastName = User;\n    timezone = nil;\n    updatedAt = \"2013-12-11 15:34:45 +0000\";\n    username = nil;\n    uuid = \"de565267-eaf9-4cbf-a63e-44dd7df405aa\";\n})";
}>

Core Data User Entity (after mapping):

(lldb) po newUser
<User: 0xc8ac300> (entity: User; id: 0xc8c5050 <x-coredata:///User/t1251A6D9-8FB8-4AF2-AFA4-ADB61F39397F2> ; data: {
    accessToken = nil;
    actions = nil;
    createdAt = nil;
    email = nil;
    firstName = nil;
    fullName = "(null) (null)";
    lastName = nil;
    timezone = nil;
    updatedAt = nil;
    username = nil;
    uuid = nil;
})

Code:

User *newUser = [User createNewUser];
NSDictionary *userParams = @{kJsonUser : params};

NSURLRequest *request = [[APIClient objectManager] multipartFormRequestWithObject:newUser method:RKRequestMethodPOST path:kEndPointCreateListUsers parameters:userParams constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    UIImage *image = [params objectForKey:kJsonImage];
    if (image) {
      [formData appendPartWithFileData:UIImageJPEGRepresentation(image, 1.0)
                                name:@"user_image"
                            fileName:@"user_image.jpeg"
                            mimeType:@"image/jpeg"];
    }
}];
RKManagedObjectRequestOperation *operation = [[APIClient objectManager] managedObjectRequestOperationWithRequest:request managedObjectContext:[APIClient mainThreadContext] success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    [self handleSignUpSuccessForUser:newUser];
    success([newUser uuid]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    [self handleSignUpFailure];
    failure([error localizedDescription]);
}];
[[APIClient objectManager] enqueueObjectRequestOperation:operation];

The one thing I've noticed is that the memory address for the User in the mapping results is different than the newUser object. If I use a normal (non-multipart) request, then it maps just fine and sets the Core Data object.

Any help you can provide is greatly appreciated!

Upvotes: 1

Views: 320

Answers (1)

Wain
Wain

Reputation: 119041

  • From the mapping result: <User: 0xc8d4360>
  • From your debug log: <User: 0xc8ac300>

So you are comparing / logging different objects.

The way you're creating the POST request means that you should destroy the newUser after making the request and take the User object provided to you in the mapping request instead.

If you were using an RKObjectManager then it would update the newUser for you but you wouldn't have an option to add the image data into the POST. Currently newUser is only used to generate the request and is not available to the mapping of the response.

Upvotes: 2

Related Questions