错误的RKResponseDescriptor - 找到嵌套对象但不可映射

时间:2014-04-19 11:14:04

标签: ios objective-c restkit

我有以下实体映射和描述符:

RKEntityMapping *responseUserMapping = [APICallUser RKGetUserMappingForManagedObjectStore:self.appDelegate.managedObjectStore];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseUserMapping
[session.objectManager addResponseDescriptor:responseDescriptor];
                                                                                            method:RKRequestMethodPOST
                                                                                       pathPattern:APICallUserCreatePattern
                                                                                           keyPath:@"user"
                                                                                       statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

以下是方法RKGetUserMappingForManagedObjectStore

的说明
+ (RKEntityMapping *) RKGetUserMappingForManagedObjectStore:(RKManagedObjectStore *) store{
    RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:store];
    userMapping.identificationAttributes = @[ @"userId" ];
    [userMapping addAttributeMappingsFromDictionary:@{
     @"id" : @"userId",
     @"email" : @"email",
     @"firstname" : @"firstName",
     @"lastname" : @"lastName",
     @"gender" : @"gender",
     @"time_zone" : @"timeZone",
     @"created_at" : @"createdAt",
     @"nickname" : @"pseudo",
     @"facebook_id" : @"facebookId",
     @"facebook_link_asked_at" : @"lastQueryForFacebookLinkDate",
     @"birthday" : @"birthDate",
     @"city" : @"city",
     @"country" : @"country",
     @"sign_in_count" : @"signInCount",
     @"facebook_token" : @"facebookToken",
     @"facebook_token_expires_at" : @"facebookExpiration",
     @"avatar.id" : @"avatarPhotoId"
     }];

    RKEntityMapping *photoMapping = [APICallPhoto RKGetPhotoMappingForManagedObjectStore:store];
    photoMapping.setNilForMissingRelationships = YES;
    [userMapping addConnectionForRelationship:@"avatarPhoto" connectedBy:@{@"avatarPhotoId" : @"photoId"}];
    //[photoMapping addConnectionForRelationship:@"avatarUsers" connectedBy:@{ @"photoId": @"avatarPhotoId" }];
    [userMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"avatar" toKeyPath:@"avatarPhoto" withMapping:photoMapping]];

    return userMapping;
}

方法RKGetPhotoMappingForManagedObjectStore

的代码
+ (RKEntityMapping *) RKGetPhotoMappingForManagedObjectStore:(RKManagedObjectStore *) store{
    RKEntityMapping *photoMapping = [RKEntityMapping mappingForEntityForName:@"Photo" inManagedObjectStore:store];
    photoMapping.identificationAttributes = @[ @"photoId" ];
    [photoMapping addAttributeMappingsFromDictionary:@{
     @"id" : @"photoId",
     @"moment_id" : @"momentId",
     @"user_id" : @"userId",
     @"title" : @"title",
     @"description" : @"photoDescription",
     @"file.thumb_url" : @"thumbnailDistURL",
     @"file.mini_url" : @"miniDistURL",
     @"file.little_url" : @"littleDistURL",
     @"file.medium_url" : @"mediumDistURL",
     @"file.public_url" : @"originalDistURL"
     }];

    /*RKEntityMapping *momentMapping = [APICallMoment RKGetMomentMappingForManagedObjectStore:store];
    [momentMapping addConnectionForRelationship:@"photos" connectedBy:@{ @"momentId": @"momentId" }];
    [photoMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"moment" toKeyPath:@"moment" withMapping:momentMapping]];


    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:photoMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"photos" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    [[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];
    */
    return photoMapping;
}

这是我的应用收到的json:

{
    "user": {
        "id": 38,
        "email": "test@test.com",
        "firstname": "bob",
        "lastname": "tonny",
        "gender": 0,
        "created_at": "2014-04-19T11:00:55Z",
        "updated_at": "2014-04-19T11:00:55Z",
        "nickname": "bobby",
        "facebook_id": null,
        "birthday": "1990-02-14",
        "city": "",
        "country": "",
        "facebook_token": null,
        "facebook_token_expires_at": null,
        "time_zone": "Europe/Paris",
        "facebook_link_asked_at": null,
        "sign_in_count": 0,
        "confirmed": false,
        "badge": {
            "permanent": 0,
            "contextual": 0
        },
        "avatar": null
    }
}

您可以在此处看到与devicedevices没有任何关系。但我有以下错误:

error=Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No mappable object representations were found at the key paths searched." UserInfo=0xd0c1770 {DetailedErrors=(
), NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: device, devices
The representation inputted to the mapper was found to contain nested object representations at the following key paths: user
This likely indicates that you have misconfigured the key paths for your mappings., NSLocalizedDescription=No mappable object representations were found at the key paths searched., keyPath=null}

我目前无法找到问题所在。

用户和设备之间的唯一关系在.xcdatamodelId上设置,如下图所示:

用户的

关系

relationship for User

设备

的关系

relationship for Device

我在描述符上花了很多时间:session.objectManager.responseDescriptors。有几个描述符,但没有关于任何设备。

如果有人能够看到我错过的地方,我真的很想知道。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。这根本不明显。将我的描述符添加到objectManager后,我做到了:

session.objectManager.requestSerializationMIMEType = RKMIMETypeJSON;

这完全解决了我的问题。我不知道为什么在这里确切是重要的,因为我已经很久以前在我的代码上设置了这个属性..

编辑:它正在运作!我没有触及任何东西,但现在它没有工作。 编辑2:我终于明白了:

我正在使用 `[session.objectManager addResponseDescriptor:responseDescriptor];     session.objectManager.requestSerializationMIMEType = RKMIMETypeJSON;

[[RKObjectManager sharedManager] managedObjectRequestOperationWithRequest:request managedObjectContext:session.objectManager.managedObjectStore.mainQueueManagedObjectContext success:^(RKObjectRequestOperation * operation,RKMappingResult * mappingResult)`

我通过变量[RKObjectManager sharedManager]更改了session.objectManager,一切正常!

我希望它会帮助别人。