这两种方法有什么区别?container.performBackgroundTask{(context)in//...dosometaskonthecontext//savethecontextdo{trycontext.save()}catch{//handleerror}}和letcontext=persistentContainer.newBackgroundContext()context.perform{//...dosometaskonthecontext//savethecontextdo{trycontext.save()}catch{//handleerror}}何时
stackoverflow中已经有类似的问题,但它对我不起作用。在我的应用程序中有一个用例,我必须观察数据库更改才能执行某些操作。为了接收更新,我订阅了NSManagedObjectContextObjectsDidChange通知(对于ViewContext)并且我打开了automaticallyMergesChangesFromParent。但是,如果我在其他上下文中更新或删除对象(使用newBackgroundContext()),我不会收到对象确实有更改通知,但它非常适合inserting新对象。你能告诉我为什么即使在启用automaticallyMergesChangesFr
在我的应用程序中,我有一个NSFetchedResultsController用于在UITableView中加载核心数据对象。与此FRC关联的提取请求使用可用于NSPersistentContainer(iOS10)的新viewContext属性。当我选择一个单元格时,我将核心数据对象传递给一个新的ViewController。这个新的VC仍然使用viewContext。从这个ViewController中,我可以更新以模态方式呈现的ViewController中的核心数据对象。为此,我将newBackgroundContext()用于模态ViewController。我可以毫无问题地