Instance Method

awakeFromInsert()

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

Declaration

func awakeFromInsert()

Discussion

You typically use this method to initialize special default property values. This method is invoked only once in the object's lifetime.

If you want to set attribute values in an implementation of this method, you should typically use primitive accessor methods (either setPrimitiveValue(_:forKey:) or—better—the appropriate custom primitive accessors). This ensures that the new values are treated as baseline values rather than being recorded as undoable changes for the properties in question.

Special Considerations

If you create a managed object then perform undo operations to bring the managed object context to a state prior to the object’s creation, then perform redo operations to bring the managed object context back to a state after the object’s creation, awakeFromInsert() is not invoked a second time.

You are typically discouraged from performing fetches within an implementation of awakeFromInsert(). Although it is allowed, execution of the fetch request can trigger the sending of internal Core Data notifications which may have unwanted side-effects. For example, in macOS, an instance of NSArrayController may end up inserting a new object into its content array twice.

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 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 willSave()

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

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.