When they are initialized both contain the same data. However once changes are applied to one of them -in this example the main- like adding, changing or deleting rows, the other will not know -in this example the background/private one-.

Therefore if we add objects to the main managedObjectContext, when we fetch the backgroundManagedObjectContext we will not get them.

How do we fix this? Well first we have to save the changes of the objectContext that was modified. We do so by adding once we’ve finalized the changes:

//Commit changes

do {

try managedObjectContext.save()

}catch{

print(“ERROR: Cound not save Core Data”)

}

At this point we have saved the changes in the Persistent Store. We still have to ask the backgroundObjectContext to get them from the persistent store thought. For that we will use the automatic notification “NSManagedObjectContextDidSave”.

In the class where the backgroundThread is contained we will add an observer for the notification (note that we’re passing as object the context that was modified):

In this case you would be showing the notification after 5 seconds, more useful though is to plan it for a later date, like you can see in my commented code. The getDateOfNextNotification function simply returns the NSDate where I would like the notification to trigger.

If you plan to use this, for instance in App Delegate, you may want to check if you have already scheduled notification to avoid duplicates. You can do the following: