我无法弄清楚如何在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.'
答案 0 :(得分:1)
您的异常可能正在发生,因为NSManagedObject
在进入请求操作时有一个临时的托管对象ID。如果需要,您可以通过获取永久ID来回到第一个实现。在必要时更新RKManagedObjectRequestOperation
以在所有情况下获取ID而不是发出警告可能是有意义的。
否则,您的子类实现看起来就像一个很好的解决方案。
答案 1 :(得分:0)
通过在RKObjectManager中创建我自己的方法来修复:
- (RKManagedObjectRequestOperation *)managedObjectRequestOperationWithRequest:(NSURLRequest *)request
object:(id)object
managedObjectContext:(NSManagedObjectContext *)managedObjectContext
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithHTTPRequestOperation:[self HTTPOperationWithRequest:request] responseDescriptors:self.responseDescriptors];
[operation setCompletionBlockWithSuccess:success failure:failure];
operation.managedObjectContext = managedObjectContext;
operation.managedObjectCache = self.managedObjectStore.managedObjectCache;
operation.fetchRequestBlocks = self.fetchRequestBlocks;
if ([object isKindOfClass:[NSManagedObject class]] && [[object objectID] isTemporaryID]) {
RKLogInfo(@"Asked to perform object request with NSManagedObject with temporary object ID: Obtaining permanent ID before proceeding.");
__block BOOL _blockSuccess;
__block NSError *_blockError;
[[object managedObjectContext] performBlockAndWait:^{
_blockSuccess = [[object managedObjectContext] obtainPermanentIDsForObjects:@[object] error:&_blockError];
}];
if (! _blockSuccess) RKLogWarning(@"Failed to obtain permanent ID for object %@: %@", object, _blockError);
}
operation.targetObject = object;
return operation;
}