我有一些困難發佈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