我有一些困难发布NSManagedObject嵌套NSManagedObjects与RestKit。当POST返回时,我似乎获取插入到CoreData中的sub-NSManagedObjects的重复记录。下面是模型的快照:RestKit发布嵌套托管对象创建重复
这里是我张贴的JSON:
{
"actions": [],
"application": "Identify",
"createBy": "welcomed",
"createDt": "2014-04-11T16:26:15Z",
"description": null,
"externalId": null,
"groupId": "5",
"id": 0,
"images": [
{
"format": "JPEG",
"height": 200,
"id": 0,
"image": "/9j/4A..../Pv5n/9k=",
"status": "C",
"type": "MUGSHOT",
"width": 200
}
],
"locked": null,
"modifyBy": null,
"modifyDt": null,
"priv": null
}
这里是从服务的POST后返回的JSON:
{
"actions": [],
"application": "Identify",
"createBy": "welcomed",
"createDt": 1397233575000,
"description": null,
"externalId": null,
"groupId": "5",
"id": 11,
"images": [
{
"captureDevice": null,
"createBy": null,
"createDt": null,
"format": "JPEG",
"height": 200,
"id": 11,
"image": "/9j/4AAQSkZJR.../Pv5n/9k=",
"recordId": 11,
"status": "C",
"type": "MUGSHOT",
"width": 200
}
],
"locked": false,
"modifyBy": null,
"modifyDt": null,
"priv": false
}
编辑(我想这很重要):这里是WTSImage和WTSRecord的映射:
RKEntityMapping *recordMapping = [RKEntityMapping mappingForEntityForName:@"WTSRecord" inManagedObjectStore:self.managedObjectStore];
[recordMapping addAttributeMappingsFromDictionary:@{
@"id":@"dbId",
@"externalId":@"extId",
@"groupId":@"groupId",
@"application": @"application",
@"description": @"desc",
@"priv": @"priv",
@"locked": @"locked",
@"createBy": @"createBy",
@"createDt": @"createDt",
@"modifyBy": @"modifyBy",
@"modifyDt": @"modifyDt",
}];
recordMapping.identificationAttributes = @[@"dbId"];
//image mapping
RKEntityMapping *imageMapping = [RKEntityMapping mappingForEntityForName:@"WTSImage" inManagedObjectStore:self.managedObjectStore];
[imageMapping addAttributeMappingsFromDictionary:@{
@"id": @"dbId",
@"status": @"status",
@"type": @"type",
@"format": @"format",
@"width": @"width",
@"height": @"height",
@"image": @"base64Image"
}];
imageMapping.identificationAttributes = @[@"dbId"];
[recordMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"images" toKeyPath:@"images" withMapping:imageMapping]];
以下代码是在哪里创建的NSManagedObjects并调用[RKObjectManager postObject:path:parameters:success:failure:
:
WTSRecord *record = [NSEntityDescription insertNewObjectForEntityForName:@"WTSRecord" inManagedObjectContext:self.managedObjectContext];
record.createBy = @"welcomed";
record.createDt = [NSDate date];
record.application = kWTSApplicationIdentify;
record.groupId = @"5";
WTSImage *image = [NSEntityDescription insertNewObjectForEntityForName:@"WTSImage" inManagedObjectContext:self.managedObjectContext];
image.height = [NSNumber numberWithFloat:mugshot.size.height];
image.width = [NSNumber numberWithFloat:mugshot.size.width];
image.imageData = UIImageJPEGRepresentation(imageData, 1.0);
image.type = kWTSCaptureTypeMugshot;
image.format = kWTSCaptureFormatJpeg;
image.status = kWTSCaptureStatusCaptured;
image.record = record;
[record addImagesObject:image];
RKObjectManager *manager = [RKObjectManager sharedManager];
[manager postObject:record path:@"records" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error Sending Record" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}];
当调用成功块,我检查SQLite数据库和有1 WTSRecord插入和 WTSImages。其中一个WTSImages对WTSRecord和数据库中的PK具有正确的FK,而另一个似乎是孤立的(dbId和FK到WTSRecord未设置)。
这里是到RestKit和核心数据跟踪日志的链接:https://dl.dropboxusercontent.com/u/466390/restkit2.txt
希望有人能帮助!谢谢!
编辑后一些更多的搜索,我找到了这个网页:https://github.com/RestKit/RestKit/issues/1228
我必须张贴到一个REST服务之前创建使用UUID客户端识别元素? RestKit无法将请求对象映射回已经在对象存储中创建的对象,而无需先在客户端设置标识属性?
我使用过的大多数Apis都允许创建一个图像容器,需要首先创建容器,然后再创建多个上传(每个图像一个)。从网络管理角度来看,这通常会更好,并且可以解决您的问题,因为每个发布的对象都会按照您的预期进行更新。 – Wain
感谢您的信息Wain!你真的是一个RestKit的神,在过去的几天里,你的答案已经为我节省了很多时间!我喜欢你的解决方案比使用客户端生成的UUID更好(我发现它能够更新嵌套的托管对象)。我宁愿不在数据库中处理额外的唯一标识符,因此我认为清理任何没有关系的图像是一种方法。感谢您确认RestKit的限制,以便我可以继续实际做一些工作。 :) – Dan
嗨@是的,这仍然是这种情况?我需要发送一堆已经存在于数据库中的相关对象,而且我更喜欢它们是否被映射,与主对象相同,从而避免了欺骗。非常感谢! – amcastror