HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – RestKit – 实体(null)不是密钥值编码兼容的密钥“id”大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
尝试使用RestKit上传图像时,我遇到了一个奇怪的问题.我研究过类似的问题,但没有一个解决我的问题.

在我的应用程序中,用户可以创建一些任务的注释

所以我设置了RKEntitymapping和RKObjectMapping映射,如下所示:

- (RKEntitymapping *)commentsmapping {

    RKEntitymapping *commentsmapping = [RKEntitymapping mappingForEntityForName:@"DBComments" inManagedObjectStore:objectManager.managedObjectStore];
    commentsmapping.setDefaultValueForMissingAttributes = NO;
    commentsmapping.identificationAttributes = @[@"id"];
    [commentsmapping setModificationAttributeForName:@"updated_at"];

    [commentsmapping addAttributeMappingsFromDictionary:@{
                                                          @"body" : @"body",@"created_at" : @"created_at",@"id" : @"id",@"parent_id" : @"parent_id",@"send_email" : @"send_email",@"updated_at" : @"updated_at",@"user.id" : @"user_id",@"user.name" : @"user_name",}];

    [commentsmapping addRelationshipMappingWithsourceKeyPath:@"attachments" mapping:[self attachmentsmapping]];

    return commentsmapping;
}


- (RKObjectMapping *)commentsrequestMapping {
    RKObjectMapping *commentsrequestMapping = [RKObjectMapping requestMapping];
    [commentsrequestMapping addAttributeMappingsFromDictionary:@{
                                                                 @"body" : @"body",}];

    return commentsrequestMapping;
}

我已经定义了这样的URL_COMMENTS:

#define URL_COMMENTS @"/comments"

为了获得对象,我打电话给:

[[RKObjectManager sharedManager] getObjectsAtPath:URL_COMMENTS
                                       parameters:@{@"item_id": parentID}
                                          success:^(RKObjectrequestOperation *operation,RKMappingResult *mappingResult) {
                                              KLog(@"success");
                                          }
                                          failure:^(RKObjectrequestOperation *operation,NSError *error) {
                                              KLog(@"fail");
                                          }];

它运行正常,没有任何错误.

如果我需要创建新评论,我打电话:

[[RKObjectManager sharedManager] postObject:comment path:URL_COMMENTS
                                 parameters:nil
                                    success:^(RKObjectrequestOperation *operation,RKMappingResult *mappingResult) {
                                        KLog(@"success");


                                } failure:^(RKObjectrequestOperation *operation,NSError *error) {
                                    KLog(@"fail");
                                }];

它也很完美,没有任何错误.

但是当我需要用图像附件创建评论时,我打电话给:

NSMutableURLrequest *request =[[RKObjectManager sharedManager] multipartFormrequestWithObject:comment
                                                                                           method:rKrequestMethodPOST
                                                                                             path:URL_COMMENTS
                                                                                       parameters:nil
                                                                        construcTingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                                                                            [formData appendPartWithFileData:UIImageJPEGRepresentation(image,0.7)
                                                                                                        name:@"photo"
                                                                                                    filename:@"photo.jpg"
                                                                                                    mimeType:@"image/jpg"];
                                                                        }];

    RKObjectrequestOperation *operation = [[RKObjectManager sharedManager] objectrequestOperationWithrequest:request
                                                                                                     success:^(RKObjectrequestOperation *operation,RKMappingResult *mappingResult) {
                                                                                                         // success handler.
                                                                                                         KLog(@"%@",[mappingResult firstObject]);

                                                                                                     } failure:^(RKObjectrequestOperation *operation,NSError *error) {
                                                                                                         KLog(@"fail");
                                                                                                     }];

    [operation start];

现在问题是,在服务器端成功创建了注释,但应用程序因错误而终止:

TerminaTing app due to uncaught exception 'NSUnkNownKeyException',reason: '[<DBComments 0x11e01400> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "id".'

这里有一些Log

D restkit.object_mapping:rKMapperOperation.m:377 ExecuTing mapping operation for representation: {
    body = "Test attachment";
    "created_at" = "2014-04-02T15:41:00+02:00";
    id = 13379;
    "parent_id" = 291160;
    "updated_at" = "2014-04-02T15:41:00+02:00";
    user =     {
        id = 12891;
        name = "Rehmat Qadir";
    };
}
 and targetObject: (null)


D restkit.object_mapping:rKMapperOperation.m:300 Found mappable data at keyPath '': {
    body = "Test attachment";
    "created_at" = "2014-04-02T15:41:00+02:00";
    id = 13379;
    "parent_id" = 291160;
    "updated_at" = "2014-04-02T15:41:00+02:00";
    user =     {
        id = 12891;
        name = "Rehmat Qadir";
    };
}



CoreData: error: Failed to call designated initializer on NsmanagedObject class 'DBComments' 
2014-04-02 18:41:00.269 MeeTingKing[14722:f03] D restkit.object_mapping:rKMapperOperation.m:231 Asked to map source object {
    body = "Test attachment";
    "created_at" = "2014-04-02T15:41:00+02:00";
    id = 13379;
    "parent_id" = 291160;
    "updated_at" = "2014-04-02T15:41:00+02:00";
    user =     {
        id = 12891;
        name = "Rehmat Qadir";
    };
} with mapping <RKEntitymapping:0xc686500 objectClass=DBComments propertymappings=(
    "<RKAttributeMapping: 0xc6877d0 user.id => user_id>","<RKAttributeMapping: 0xc6875c0 id => id>","<RKAttributeMapping: 0xc6875e0 send_email => send_email>","<RKAttributeMapping: 0xc687610 created_at => created_at>","<RKAttributeMapping: 0xc687630 parent_id => parent_id>","<RKAttributeMapping: 0xc6877e0 updated_at => updated_at>","<RKAttributeMapping: 0xc687850 user.name => user_name>","<RKAttributeMapping: 0xc687920 body => body>","<RKRelationshipMapping: 0xc688b90 attachments => attachments>"
)>


D restkit.object_mapping:rKMappingOperation.m:952 StarTing mapping operation...


*** TerminaTing app due to uncaught exception 'NSUnkNownKeyException',reason: '[<DBComments 0x11e01400> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "id".'

我认为它可能与某些URL问题有关.
我在URL_COMMENTS发送请求,该请求定义为#define URL_COMMENTS @“/ comments”

我试过通过发送请求注释(没有开始/)并在responseDescriptor中将pathPattern设置为/ comments(带开头/)

我也尝试过发送请求/注释(带开头/)并在responseDescriptor中设置pathPattern作为注释(不包括开头/)

[RKResponseDescriptor responseDescriptorWithMapping:[self commentsmapping]
                                                                                                  method:rKrequestMethodGET
                                                                                             pathPattern:URL_COMMENTS
                                                                                                 keyPath:@"" statusCodes:rKStatusCodeIndexSetForClass(RKStatusCodeClasssuccessful)],

也尝试通过设置两个相同的地方有或没有/但没有任何工作.

我之前需要调用AFNetworking方法时,我已经通过路径的某种组合修复了这个错误.但不知道为什么会发生这种错误.

请帮忙

解决方法

因为注释是一个托管对象,并且您要求RestKit将其视为普通对象.因此,RestKit正在尝试创建一个新实例,在该实例中设置响应数据并在创建时失败.

您应该使用managedObjectrequestOperationWithrequest:managedObjectContext:成功:失败:创建操作,使得它具有对MOC的引用,并能正确地创建实体实例(尽管它应该找到在这种情况下,现有的实体实例).

大佬总结

以上是大佬教程为你收集整理的ios – RestKit – 实体(null)不是密钥值编码兼容的密钥“id”全部内容,希望文章能够帮你解决ios – RestKit – 实体(null)不是密钥值编码兼容的密钥“id”所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。