Contents

transactionAuthor

The author for the context that is used as an identifier in persistent history transactions.

Declaration

var transactionAuthor: String? { get set }

Mentioned in

Discussion

Set a managed object context’s transactionAuthor before saving it to differentiate among multiple call sites that modify the same context. Doing this records an author in subsequent transactions.

func addColor(_ name: String, in context: NSManagedObjectContext) {
    let color = Color(context: context)
    color.name = name
    color.creationDate = Date()

    // set the transaction author
    context.transactionAuthor = "addColor"
    persistentContainer.saveContext(context)
    context.transactionAuthor = nil
}

Reset the context’s transactionAuthor to nil after the save to prevent misattribution of future transactions.

See Also

Managing concurrency