5

为了保存我当前NSManagedObjectContext使用[localContext MR_saveNestedContexts];的,但我收到警告,该方法已被弃用。

我应该如何保存NSManagedObjectContext最新版本的 Magical Record(字面意思是今天从 GitHub 提取,2013 年 7 月 19 日)。

4

1 回答 1

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];
于 2013-07-19T21:39:09.120 回答