How to determine when viewContext NSPsistentContainer finished merging changes with another NSManagedObjectContext?

I use NSPersistentContainerto control the application master data stack. As you know, it has a property viewContextthat is bound to the main queue in the main thread of the application user interface. I use it to read data in a database. To write new data (done in the background NSOperation) I use the background context through newBackgroundContext()and then the save()background context to the persistent storage.

Now the documentation viewContextsays the following:

This context is configured as a generator and automatically uses save notifications from other contexts.

Apple's docs no longer have information on how it uses persistent notifications, but I suppose it NSPersistentContainerinternally does mergeChangesFromContextDidSaveNotification:on viewContext. I want to be notified when the merge is complete viewContextso that I can run the function in the controllers of my view in order to retrieve the updated data and display it.

However, I do not know how to do this. There is no such thing as an NSManagedObjectContext delegate. I also think that listening NSManagedObjectContextDidSaveNotificationis not the way to go, since it starts when the context is saved, and not when viewContextthe merge of the changes with the save completes.

+4
source share

Source: https://habr.com/ru/post/1669101/


All Articles