top of page

NSManagedObjectContext().refreshAllObjects()

CoreData Documentation

Persist or cache data on a single device or sync data to multiple devices with CloudKit.


Many of Apple platform developers don't get to this point. You only get here if you have a mature application with some customers, hopefully. I can say projects that are proof of concept, or MVP might be far off from an offline feature, or maybe not.


So what's does this refreshAllObjects() method does? When you call this method, it will cause all managed objects that are currently registered with the managed object context to be reset, discarding any unsaved changes and fetching the latest values from their persistent store.


This is useful when you have multiple contexts and you want to ensure that changes made in one context are reflected in another context. For example, you may have a background context that updates data from a remote server, and you want to ensure that the main context, which is responsible for displaying the data to the user, is always up-to-date.


It's important to note that calling "refreshAllObjects" can have performance implications, as it will cause all registered objects to be refetched from their persistent store. Therefore, you should use this method judiciously and only when necessary to ensure that your app's data is up-to-date.


let contextToUse = newBackgroundContext

await contextToUse.perform {
	// perform work
	try contextToUse.save()
}

viewContext.refreshAllObjects()


bottom of page