我在尝试使用 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",
@"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",
}];
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"
我已经尝试通过在 comments
发送请求(没有开头/)并将 responseDescriptor
中的 pathPattern
设置为 /comments
(以/开头)
我也尝试过发送请求 /comments
(以/开头)并将 responseDescriptor
中的 pathPattern
设置为 comments
(无开头/)
[RKResponseDescriptor responseDescriptorWithMapping:[self commentsMapping]
method:RKRequestMethodGET
pathPattern:URL_COMMENTS
keyPath:@"" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)],
也尝试将两个位置设置为相同,有或没有 /
但没有任何效果。
每当我需要调用 AFNetworking
方法时,我之前已经通过路径中的 /
的某种组合修复了这个错误。但是不知道为什么会出现这个错误。
请帮忙
最佳答案
因为评论是一个托管对象,而您要求 RestKit 将其视为普通对象。因此,RestKit 正在尝试创建一个新实例来设置响应数据,但创建失败。
您应该使用 managedObjectRequestOperationWithRequest:managedObjectContext:success:failure:
创建操作,以便它具有对 MOC 的引用并可以正确创建实体实例(尽管它应该找到现有实体本例中的实例)。
关于ios - RestKit - 实体(null)与键 "id"的键值编码不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22814233/