Search code examples
iosobjective-ccore-datarestkitrestkit-0.20

RestKit .20 CoreData postObject EXC_BAD_ACCESS


I'm attempting to use RestKit to handle posting an updated User object to my remote web service.

Currently my GET requests seem to be working fine but I'm having issues using [[RKObjectManager sharedManager] postObject:updatedUser path:@"path" parameters:nil success:nil failure:nil];

Invoking this method is throwing a EXC_BAD_ACCESS exception.

My mappings are set up as follows, I believe I have both the RKRequestDescriptor and RKResponseDescriptor's.

User Response Mapping:

RKEntityMapping * userMapping =
[RKEntityMapping mappingForEntityForName:NSStringFromClass([User class])
inManagedObjectStore:[manager managedObjectStore]];

….Setup mapping (I excluded a relationship mapping on this object)

[manager addResponseDescriptorsFromArray:@[
        [RKResponseDescriptor responseDescriptorWithMapping:userMapping
                                                     method:RKRequestMethodGET
                                                pathPattern:nil 
                                                    keyPath:@"currentUser"
                                                statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]]]

Request mapping:

[manager addRequestDescriptorsFromArray:@[
        [RKRequestDescriptor requestDescriptorWithMapping:[userMapping inverseMapping]
                                              objectClass:[User class]
                                              rootKeyPath:nil
                                                   method:RKRequestMethodPOST]]];

The mappings seem to set up fine, the EXC_BAD_ACCESS exception is thrown when I call postObject

The test method looks like this, _updatedUser is a CoreData object fetched using [RKObjectManager sharedManager] getObjectsAtPath:…

-(void) doPost{
   //this user is a CoreData object fetched using 
    [_updatedUser setBio:@"This is an update!"];

    RKObjectManager * objectManager = [RKObjectManager sharedManager];
    [objectManager postObject:_updatedUser
                         path:@"update/user"
                   parameters:nil
                      success:…
                      failure:…];

  }

I've attempted using NSZombies to find the cause of this but I have't had much luck.

From what I can tell the start of the issue seems to be coming from RKObjectParameterization's -[RKObjectParameterization mappingOperation:didSetValue:forKeyPath:usingMapping:] where it looks like everything passed into the method is nil or an empty string.

Thanks!


Solution

  • Much thanks to Wain, after spending way too much time on this the error became instantly apparent after I turned on logging:

    RKLogConfigureByName("RestKit", RKLogLevelWarning);
    RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
    RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
    

    It turns out I had a circular reference between mapped objects.

    I have a one to one relationship where a User contains a UserProfile

    I incorrectly set up a bidirectional relationship mapping between User and UserProfile

    [userProfileMapping addPropertyMapping:[RKRelationshipMapping
                      relationshipMappingFromKeyPath:@"user"
                                           toKeyPath:@"user" 
                                         withMapping:userMapping]];
    
    [userMapping addPropertyMapping:[RKRelationshipMapping  relationshipMappingFromKeyPath:@"userProfile"
                     toKeyPath:@"userProfile"
                   withMapping:userProfileMapping]];
    

    It looks like my endless loop was caused by userProfileMapping

    Thanks Wain, logging lesson learned.