RestKit POSTing嵌套的托管对象创建重复项

时间:2014-04-11 17:04:30

标签: ios core-data restkit relationship restkit-0.20

我在使用RestKit嵌套NSManagedObjects的NSManagedObject时遇到一些困难。我似乎在POST返回时获取插入CoreData的子NSManagedObjects的重复记录。以下是该模型的快照:

Model

这是我正在发布的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和 2 WTSImages。其中一个WTSImages具有正确的FTS到WTSRecord和来自数据库的PK,而另一个似乎是孤立的(dbId和FK到WTSRecord没有设置)。

以下是RestKit和Core Data跟踪日志的链接:https://dl.dropboxusercontent.com/u/466390/restkit2.txt

希望有人可以提供帮助!谢谢!

编辑经过多次搜索后,我找到了此页面:https://github.com/RestKit/RestKit/issues/1228

在将它们发布到REST服务之前,是否必须使用UUID在客户端创建标识元素?在没有首先在客户端设置标识属性的情况下,RestKit是否无法将请求对象映射回已经在对象存储中创建的对象?

1 个答案:

答案 0 :(得分:1)

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

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

重复删除:

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