ios – 使用最新版魔法记录保存NSManagedContext的正确方法

ios – 使用最新版魔法记录保存NSManagedContext的正确方法,第1张

概述为了保存我当前的NSManagedObjectContext,我使用[localContext MR_saveNestedContexts];但我得到一个警告,该方法已被弃用. 我应该如何使用最新版本的魔法记录保存NSManagedObjectContext(字面意思是今天从GitHub中提取,2013年7月19日). 查看他们的文档. https://github.com/magicalpand 为了保存我当前的NSManagedobjectContext,我使用[localContext MR_savenestedContexts];但我得到一个警告,该方法已被弃用.

我应该如何使用最新版本的魔法记录保存NSManagedobjectContext(字面意思是今天从GitHub中提取,2013年7月19日).

解决方法 查看他们的文档.
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];
总结

以上是内存溢出为你收集整理的ios – 使用最新版魔法记录保存NSManagedContext的正确方法全部内容,希望文章能够帮你解决ios – 使用最新版魔法记录保存NSManagedContext的正确方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1068986.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存