RestKit .20 CoreData postObject EXC_BAD_ACCESS

时间:2014-08-03 17:06:47

标签: ios objective-c core-data restkit restkit-0.20

我尝试使用RestKit处理将更新的User对象发布到我的远程Web服务。

目前我的GET请求似乎工作正常,但我遇到了问题 [[RKObjectManager sharedManager] postObject:updatedUser path:@"path" parameters:nil success:nil failure:nil];

调用此方法会引发EXC_BAD_ACCESS异常。

我的映射设置如下,我相信我同时拥有RKRequestDescriptorRKResponseDescriptor

用户响应映射:

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

.... .Setup mapping(我排除了这个对象的关系映射)

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

请求映射:

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

映射似乎设置正常,当我调用EXC_BAD_ACCESS

时会抛出postObject异常

测试方法如下所示,_updatedUser是使用[RKObjectManager sharedManager] getObjectsAtPath:…

获取的CoreData对象
-(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:…];

  }

我尝试使用NSZombies查找原因但我没有运气。

据我所知,问题的开头似乎来自RKObjectParameterization&#39; -[RKObjectParameterization mappingOperation:didSetValue:forKeyPath:usingMapping:],其中传递给方法的所有内容都是nil或空字符串。< / p>

谢谢!

1 个答案:

答案 0 :(得分:1)

非常感谢Wain,在花了太多时间之后,在我打开日志记录后,错误立即变得明显:

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

事实证明,我在映射对象之间有一个循环引用。

我有一对一的关系,其中User包含UserProfile

我错误地设置了UserUserProfile

之间的双向关系映射
[userProfileMapping addPropertyMapping:[RKRelationshipMapping
                  relationshipMappingFromKeyPath:@"user"
                                       toKeyPath:@"user" 
                                     withMapping:userMapping]];

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

看起来我的无限循环是由userProfileMapping引起的

谢谢Wain,记录了所学的课程。