我有一个带有一些属性的NSManagedObject
(文件),我根据此AFNetworking
中的信息编写了一些代码,用NSManagedObject
下载文件 - 下载工作正常!
现在,我想更改completion block
中的一些MetaData,并使用CoreData
将更改保存到Magical Record
。这基本上有效,除了我必须重新启动App才能获得NSManagedObject
的新值 - 这就是问题所在。在一个生命周期中,我无法获得更新的NSManagedObject
。我甚至试图重新获取对象......
我不明白,为什么我必须重新启动App以获取更新信息?是否有context
问题?我没有看到它。
我从defaultContext
获取文件对象:
NSManagedObjectContext *managedObjectContext = [NSManagedObjectContext MR_defaultContext];
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:@"File" inManagedObjectContext:managedObjectContext]];
这是我的下载方法:
- (void)downloadFile:(File*)fileObject {
...
completionBlock:^(AFHTTPRequestOperation *operation, id responseObject) {
[MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
File *localFile = [fileObject MR_inContext:localContext];
localFile.downloadStatus_ = [NSNumber numberWithInt:DownloadStatusCompleted];
}];
NSLog(@"State: %@"fileObject.downloadStatus_);
}
}
更新: 我改变了线路并且工作正常。我很困惑。我猜,localContext实际上应该是当前的上下文。哪个应该是对象的那个。
File *localFile = [fileObject MR_inContext:localContext];
到
File *localFile = [fileObject MR_inContext:[NSManagedObjectContext MR_defaultContext]];
答案 0 :(得分:0)
I figured out what happened and wanted to post the answer.
It turned out that a property of the NSManagedObject *fileObject was changed without saving it, in advance of calling the completionBlock! The StoreCoordinator tries to merge and save the changed property (within the completionBlock) - but this operation failed since the original object from the context was changed...