我遇到一些带有restkit的nsmanagedObject的NSManagedObject困难。返回后,我似乎是为插入Coredata的子NSManagedObjects的重复记录。这是模型的快照:

这里是我发布的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
}
.

这是帖子后从服务返回的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和call [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和 2 wtsimages。其中一个wtsimages向WTSRecord和来自数据库的PK具有正确的FK,而另一个似乎是孤立(DBID和FK到WTSRecord未设置)。

此处是恢复和核心数据跟踪日志的链接: https:// dl.dropboxusercontent.com/u/466390/restkit2.txt

希望有人可以帮忙!谢谢!

编辑在一些搜索之后,我找到了这个页面: https ://github.com/restkit/restkit/issues/1228

在将它们发布到REST服务之前,我是否必须使用UUID在客户端创建标识元素?将restkit将无法将请求对象映射回对象存储中已创建的对象,而无需在客户端上设置标识属性?

有帮助吗?

解决方案

对于已发布的对象,Restkit了解如何使用响应数据更新该项目,但这不适用于关系内容。从技术上讲,它可以编码,但它目前不是。

如果在映射后需要在关系中的对象是与您创建的相同对象,则您有一个问题。如果您不介意它是一个不同的对象,那么问题只是删除重复...

重复删除:

在处理后响应时不使用fetch请求块,因此您需要获取欺骗并手动将其删除。我将假设任何具有零与记录关系的图像是一个欺骗,所以它是一个相对简单的获取来执行。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top