Instance Method

refresh(_:mergeChanges:)

Updates the persistent properties of a managed object to use the latest values from the persistent store.

Declaration

func refresh(_ object: NSManagedObject, mergeChanges flag: Bool)

Parameters

object

A managed object.

flag

A Boolean value.

If flag is false, then object is turned into a fault and any pending changes are lost. The object remains a fault until it is accessed again, at which time its property values will be reloaded from the store or last cached state.

If flag is true, then object is turned into a fault and object’s property values are reloaded from the values from the store or the last cached state then any changes that were made (in the local context) are re-applied over those (now newly updated) values. (If flag is true the merge of the values into object will always succeed—in this case there is therefore no such thing as a “merge conflict” or a merge that is not possible.)

Discussion

If the staleness interval (see stalenessInterval) has not been exceeded, any available cached data is reused instead of executing a new fetch. If flag is true, this method does not affect any transient properties; if flag is false, transient properties are disposed of.

You typically use this method to ensure data freshness if more than one managed object context may use the same persistent store simultaneously, in particular if you get an optimistic locking failure when attempting to save.

Turning object into a fault (flag is false) means that strong references to related managed objects (that is, those to which object has a reference) are broken, so you can also use this method to trim a portion of your object graph you want to constrain memory usage.

See Also

Managing Managed Objects

var shouldDeleteInaccessibleFaults: Bool

A Boolean value that indicates the behavior of inaccessible faults.

var insertedObjects: Set<NSManagedObject>

The set of objects that have been inserted into the context but not yet saved in a persistent store.

var updatedObjects: Set<NSManagedObject>

The set of objects registered with the context that have uncommitted changes.

var deletedObjects: Set<NSManagedObject>

The set of objects that will be removed from their persistent store during the next save operation.

func insert(NSManagedObject)

Registers an object to be inserted in the context’s persistent store the next time changes are saved.

func delete(NSManagedObject)

Specifies an object that should be removed from its persistent store when changes are committed.

func assign(Any, to: NSPersistentStore)

Specifies the store in which a newly inserted object will be saved.

func obtainPermanentIDs(for: [NSManagedObject])

Converts to permanent IDs the object IDs of the objects in a given array.

func detectConflicts(for: NSManagedObject)

Marks an object for conflict detection.

func processPendingChanges()

Forces the context to process changes to the object graph.

func observeValue(forKeyPath: String?, of: Any?, change: [String : Any]?, context: UnsafeMutableRawPointer?)

Allows a context that has registered as an observer of a value to be notified of a change to that value.