1
我正在嘗試執行多部分POST以創建具有附加圖像的新用戶。 RKMappingResult
顯示它已成功映射響應,但由於某種原因,它沒有正確映射到User
實體對象,因爲映射後所有這些屬性仍爲nil
。已填充RestKit映射結果,但未設置核心數據實體
RKMappingResult:
(lldb) po mappingResult
<RKMappingResult: 0xc8d04a0, results={
user = "<User: 0xc8d4360> (entity: User; id: 0xc676560 <x-coredata://68DA437E-D836-48F7-B4CE-CC08EC38B98F/User/p15> ; data: {\n accessToken = c81453227a39e16e396218d3a1ef0d80;\n actions = nil;\n createdAt = \"2013-12-11 15:34:45 +0000\";\n email = \"[email protected]\";\n firstName = Some;\n fullName = \"Some User\";\n lastName = User;\n timezone = nil;\n updatedAt = \"2013-12-11 15:34:45 +0000\";\n username = nil;\n uuid = \"de565267-eaf9-4cbf-a63e-44dd7df405aa\";\n})";
}>
核心數據User
實體(映射後):
(lldb) po newUser
<User: 0xc8ac300> (entity: User; id: 0xc8c5050 <x-coredata:///User/t1251A6D9-8FB8-4AF2-AFA4-ADB61F39397F2> ; data: {
accessToken = nil;
actions = nil;
createdAt = nil;
email = nil;
firstName = nil;
fullName = "(null) (null)";
lastName = nil;
timezone = nil;
updatedAt = nil;
username = nil;
uuid = nil;
})
代碼:
User *newUser = [User createNewUser];
NSDictionary *userParams = @{kJsonUser : params};
NSURLRequest *request = [[APIClient objectManager] multipartFormRequestWithObject:newUser method:RKRequestMethodPOST path:kEndPointCreateListUsers parameters:userParams constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
UIImage *image = [params objectForKey:kJsonImage];
if (image) {
[formData appendPartWithFileData:UIImageJPEGRepresentation(image, 1.0)
name:@"user_image"
fileName:@"user_image.jpeg"
mimeType:@"image/jpeg"];
}
}];
RKManagedObjectRequestOperation *operation = [[APIClient objectManager] managedObjectRequestOperationWithRequest:request managedObjectContext:[APIClient mainThreadContext] success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
[self handleSignUpSuccessForUser:newUser];
success([newUser uuid]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
[self handleSignUpFailure];
failure([error localizedDescription]);
}];
[[APIClient objectManager] enqueueObjectRequestOperation:operation];
有一兩件事我注意到的是,在內存地址爲User
中的映射結果是不同的newUser
對象。如果我使用正常(非多部分)請求,那麼它會很好地映射並設置Core Data對象。
任何幫助,您可以提供非常感謝!
感謝您的快速響應!有沒有更好的方法來創建請求,以便我使用的[RKObjectManager](存儲在[APIClient objectManager]中)將更新'newUser',而不必刪除它並使用映射結果? – ZeNewb
我不這麼認爲 - 只是因爲你正在發送圖像數據。它的形象沒有被髮送,然後是的。 – Wain
很酷,非常感謝您的幫助!我不再傳入一個對象,然後使用User * newUser = [mappingResult firstObject];'現在獲取新創建的用戶。 – ZeNewb