【发布时间】:2014-03-17 19:19:48
【问题描述】:
我正在使用 AFNetworking 获取远程数据,并使用 Magical Record 将其导入并保存在本地。
基本上,我在 AFNetworking 方法的完成块内使用块调用神奇的记录保存,这导致我的单元测试永远挂起。
这里是同步方法的代码。它在 SyncEngine 类中定义,用作单例。
NSArray *operations = … ; // An array of AFHTTPRequestOperation defined before
__block BOOL syncSuccess = … ; // Set by operations
__block NSDictionary *syncErrors = … ; // Set by operations
NSArray *batchedOperations =
[AFURLConnectionOperation batchOfRequestOperations:operations
progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
// …
}
completionBlock:^(NSArray *operations) {
NSMutableDictionary *allErrors = [NSMutableDictionary dictionaryWithDictionary:syncErrors];
[_localContext saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error){
if (!success && error)
allErrors[@"MR_saveToPersistentStore"] = error;
syncCompletionBlock(syncSuccess && success, allErrors);
}];
}
];
// _manager is a AFHTTPRequestOperationManager
[_manager.operationQueue addOperations:batchedOperations waitUntilFinished:YES];
所以问题出在我在“completionBlock”中调用“saveToPersistentStoreWithCompletion”这一事实。
如果我改用“saveToPersistentStoreAndWait”,它可以工作,但我不知道保存是否成功。
嵌套这些操作的正确方法是什么?
【问题讨论】:
标签: objective-c nsmanagedobjectcontext magicalrecord afnetworking-2