Core Data viewContext not receiving updates from newBackgroundContext() with NSFetchedResultsController

后端 未结 4 2026
一生所求
一生所求 2021-01-30 16:59

In my application, I have a NSFetchedResultsController to load Core Data objects in a UITableView. The fetch request associated with this FRC uses the new vie

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-30 17:50

    Not worked for me, So I changed save block from

    self.persistentContainer.performBackgroundTask { (context) ... }
    

    to

    self.persistentContainer.newBackgroundContext().performAndWait { ... }
    

    and of corse setting true automaticallyMergesChangesFromParent to make auto merge work.

    lazy var viewContext: NSManagedObjectContext = {
        self.persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
        self.presistentContainer.viewContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump
        return self.persistentContainer.viewContext
    }()
    

    I don't know why the context from performBackgroundTask not merged to viewContext.

提交回复
热议问题