Instance Method

willSave()

Provides an opportunity to add code into the life cycle of the managed object when it is about to be saved.

Declaration

func willSave()

Discussion

This method can have “side effects” on persistent values. You can use it to, for example, compute persistent values from other transient or scratchpad values.

If you want to update a persistent property value, you should typically test for equality of any new value with the existing value before making a change. If you change property values using standard accessor methods, Core Data will observe the resultant change notification and so invoke willSave again before saving the object’s managed object context. If you continue to modify a value in willSave, willSave will continue to be called until your program crashes.

For example, if you set a last-modified timestamp, you should check whether either you previously set it in the same save operation, or that the existing timestamp is not less than a small delta from the current time. Typically it’s better to calculate the timestamp once for all the objects being saved (for example, in response to an NSManagedObjectContextWillSaveNotification).

If you change property values using primitive accessors, you avoid the possibility of infinite recursion, but Core Data will not notice the change you make.

The sense of “save” in the method name is that of a database commit statement and so applies to deletions as well as to updates to objects. For subclasses, this method is therefore an appropriate locus for code to be executed when an object deleted as well as “saved to disk.” You can find out if an object is marked for deletion with isDeleted.

See Also

Managing Change Events

class var contextShouldIgnoreUnmodeledPropertyChanges: Bool

A Boolean value that indicates whether instances of the class should be marked as having changes if an unmodeled property is changed.

func awakeFromFetch()

Provides an opportunity to add code into the life cycle of the managed object when it is being fulfilled from a fault.

func awakeFromInsert()

Provides an opportunity to add code into the life cycle of the managed object when it is initially created.

func awake(fromSnapshotEvents: NSSnapshotEventType)

Provides an opportunity to add code into the life cycle of the managed object when it is being fulfilled from a snapshot.

func changedValues() -> [String : Any]

Returns a dictionary containing the keys and (old) values of persistent properties that have been changed since the last fetching or saving of the managed object.

func changedValuesForCurrentEvent() -> [String : Any]

Returns a dictionary containing the keys and old values of persistent properties that have changed since the last posting of NSManagedObjectContextObjectsDidChange.

func committedValues(forKeys: [String]?) -> [String : Any]

Returns a dictionary of the last fetched or saved values of the managed object for the properties specified by the given keys.

func prepareForDeletion()

Provides an opportunity to add code into the life cycle of the managed object when it is about to be deleted.

func didSave()

Provides an opportunity to add code into the life cycle of the managed object after the managed object’s context completes a save operation.

func willTurnIntoFault()

Provides an opportunity to add code into the life cycle of the managed object before it is converted to a fault.

func didTurnIntoFault()

Provides an opportunity to add code into the life cycle of the managed object when it is turned into a fault.

class func fetchRequest() -> NSFetchRequest<NSFetchRequestResult>

Returns a new fetch request initialized with the entity represented by this subclass.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software