我不知道如何在 postOperation 上设置 targetObject。现在我所有的 postObject 方法都工作正常,并将响应数据从服务器映射到我的对象。但是 managedObjectRequestOperationWithRequest 并没有这样做,并且我为 targetObject 拥有的日志返回 null。
我尝试手动设置它(请参阅下面的代码注释) - 但随后我收到一条错误消息,提示托管对象上下文不匹配(一个为空)。这是因为线程吗?无论如何围绕这个?
// post image
// create request
postRequest = [RKObjectManager.sharedManager multipartFormRequestWithObject:media method:RKRequestMethodPOST path:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:media.imageData name:@"userfile[]" fileName:filename mimeType:@"image/jpeg"];
}];
// create operation
RKManagedObjectRequestOperation *postOperation = [RKObjectManager.sharedManager managedObjectRequestOperationWithRequest:postRequest managedObjectContext:viewController.managedObjectContext success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"operation.targetObject: %@", operation.targetObject);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"post image error: %@", error);
}];
// postOperation.targetObject = media;
// enqueue operation
[RKObjectManager.sharedManager enqueueObjectRequestOperation:postOperation];
// monitor upload progress
[postOperation.HTTPRequestOperation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"bytesWritten: %d, totalBytesWritten: %lld, totalBytesExpectedToWrite: %lld", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
}];
这是我在注释掉 postOperation.targetObject = media; 时收到的错误。
2012-12-14 20:04:36.256 Keepsayk Alpha[1966:1503] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Serious Core Data error: requested existing object with ID 0x1f805a30 <x-coredata:///Media/t0FEC53CF-F383-4E7E-8568-38F42EA839453> in context <NSManagedObjectContext: 0x1fa9a790>, instead got an object reference in context (null). This may indicate that the objectID for your target managed object was obtained using `obtainPermanentIDsForObjects:error:` in the wrong context.'