为了保存我当前的NSManagedObjectContext
我使用[localContext MR_saveNestedContexts];
,但我收到一条警告,说明该方法已被弃用。
我应该如何使用最新版本的魔法记录保存NSManagedObjectContext
(今天从2013年7月19日起从GitHub中提取)。
答案 0 :(得分:5)
查看他们的文件 https://github.com/magicalpanda/MagicalRecord/blob/master/Docs/Saving-Entities.md
此外,当我过去向他们提问时,他们反响敏捷。你也可以随时尝试。
编辑:
不知道为什么我得到了投票。也许文档太混乱了。尝试使用
- (void) MR_saveToPersistentStoreWithCompletion:(MRSaveCompletionHandler)completion;
我没有使用最新版本的MagicalRecord,但我认为这应该是正确的
//get the context for the current thread
//this context can be updated by anyone other process on this thread that uses the same MR_contextForCurrentThread call
//it's a local store that can be merged to a parent store
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
//create an NSManagedObject of type YourEntity and insert it into the localContext object
NSManagedObject *obj = [YourEntity MR_createInContext:localContext];
//make any updates to obj
//save the localContext async
//this call should save all nested NSManagedObjectContexts as well (if they exist)
[localContext MR_saveToPersistentStoreWithCompletion:^{
//when the async save is complete, this block gets executed
//blocks work very similarly to javascript callbacks
//basically it's a function reference or block of code that get's packaged up and can be passed around
//In this case, the completion block allows to to run any code after the save has been completed.
}];
我开始时没有意识到的一件事是当我创建我的实体时,它也将它插入到上下文中。它导致我不小心保存了我不需要保留的对象。为了避免这种情况,我设置了一个子上下文,只在我想要保留对象时保存它。
self.context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
self.context.parentContext = [NSManagedObjectContext MR_defaultContext];