2014-04-11 30 views
1

我有一些困難發佈NSManagedObject嵌套NSManagedObjects與RestKit。當POST返回時,我似乎獲取插入到CoreData中的sub-NSManagedObjects的重複記錄。下面是模型的快照:RestKit發佈嵌套託管對象創建重複

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插入和 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無法將請求對象映射回已經在對象存儲中創建的對象,而無需先在客戶端設置標識屬性?

回答

1

對於發佈的對象,RestKit瞭解如何使用響應數據更新該項目,但不適用於關係內容。從技術上講,它可以被編碼,但它目前不是。

如果您需要映射後關係中的對象與您創建的對象相同,那麼您有問題。如果你不介意它是一個不同的對象,則問題只是刪除重複...

重複刪除:

獲取請求塊處理POST響應所以你最好不使用時,需要獲取並手動刪除它。我將假設任何與記錄無關的圖像都是一個愚蠢的行爲,因此這是一個相對簡單的執行獲取。

+1

我使用過的大多數Apis都允許創建一個圖像容器,需要首先創建容器,然後再創建多個上傳(每個圖像一個)。從網絡管理角度來看,這通常會更好,並且可以解決您的問題,因爲每個發佈的對象都會按照您的預期進行更新。 – Wain

+0

感謝您的信息Wain!你真的是一個RestKit的神,在過去的幾天裏,你的答案已經爲我節省了很多時間!我喜歡你的解決方案比使用客戶端生成的UUID更好(我發現它能夠更新嵌套的託管對象)。我寧願不在數據庫中處理額外的唯一標識符,因此我認爲清理任何沒有關係的圖像是一種方法。感謝您確認RestKit的限制,以便我可以繼續實際做一些工作。 :) – Dan

+0

嗨@是的,這仍然是這種情況?我需要發送一堆已經存在於數據庫中的相關對象,而且我更喜歡它們是否被映射,與主對象相同,從而避免了欺騙。非常感謝! – amcastror