Update NSFetchedResultsController using performBackgroundTask

前端 未结 3 1255
粉色の甜心
粉色の甜心 2020-12-13 10:21

I have an NSFetchedResultsController and I am trying to update my data on a background context. For example, here I am trying to delete an object:



        
3条回答
  •  误落风尘
    2020-12-13 10:53

    The view context will not update unless you have set it to automatically merge changes from the parent. The viewContext is already set as child of any backgroundContext that you receive from the NSPersistentContainer.

    Try adding just this one line:

    persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
    

    Now, the viewContext WILL update after the backgroundContext has been saved and this WILL trigger the NSFetchedResultsController to update.

提交回复
热议问题