BaseDataTransaction

public class BaseDataTransaction

The BaseDataTransaction is an abstract interface for NSManagedObject creates, updates, and deletes. All BaseDataTransaction subclasses manage a private NSManagedObjectContext which are direct children of the NSPersistentStoreCoordinator‘s root NSManagedObjectContext. This means that all updates are saved first to the persistent store, and then propagated up to the read-only NSManagedObjectContext.

  • Indicates if the transaction has pending changes

    Declaration

    Swift

    public var hasChanges: Bool { get }
  • Creates a new NSManagedObject or CoreStoreObject with the specified entity type.

    Declaration

    Swift

    public func create<D>(_ into: Into<D>) -> D where D : DynamicObject

    Parameters

    into

    the Into clause indicating the destination NSManagedObject or CoreStoreObject entity type and the destination configuration

    Return Value

    a new NSManagedObject or CoreStoreObject instance of the specified entity type.

  • Returns an editable proxy of a specified NSManagedObject or CoreStoreObject.

    Declaration

    Swift

    public func edit<D>(_ object: D?) -> D? where D : DynamicObject

    Parameters

    object

    the NSManagedObject or CoreStoreObject type to be edited

    Return Value

    an editable proxy for the specified NSManagedObject or CoreStoreObject.

  • Returns an editable proxy of the object with the specified NSManagedObjectID.

    Declaration

    Swift

    public func edit<D>(_ into: Into<D>, _ objectID: NSManagedObjectID) -> D? where D : DynamicObject

    Parameters

    into

    an Into clause specifying the entity type

    objectID

    the NSManagedObjectID for the object to be edited

    Return Value

    an editable proxy for the specified NSManagedObject or CoreStoreObject.

  • Deletes a specified NSManagedObject or CoreStoreObject.

    Declaration

    Swift

    public func delete<D>(_ object: D?) where D : DynamicObject

    Parameters

    object

    the NSManagedObject or CoreStoreObject to be deleted

  • Deletes the specified NSManagedObjects or CoreStoreObjects.

    Declaration

    Swift

    public func delete<D>(_ object1: D?, _ object2: D?, _ objects: D?...) where D : DynamicObject

    Parameters

    object1

    the NSManagedObject or CoreStoreObject to be deleted

    object2

    another NSManagedObject or CoreStoreObject to be deleted

    objects

    other NSManagedObjects or CoreStoreObjects to be deleted

  • Deletes the specified NSManagedObjects or CoreStoreObjects.

    Declaration

    Swift

    public func delete<S>(_ objects: S) where S : Sequence, S.Element : DynamicObject

    Parameters

    objects

    the NSManagedObjects or CoreStoreObjects to be deleted

  • Refreshes all registered objects NSManagedObjects in the transaction.

    Declaration

    Swift

    public func refreshAndMergeAllObjects()
  • Returns all pending DynamicObjects of the specified type that were inserted to the transaction. This method should not be called after the commit() method was called.

    Declaration

    Swift

    public func insertedObjects<D>(_ entity: D.Type) -> Set<D> where D : DynamicObject, D : Hashable

    Parameters

    entity

    the DynamicObject subclass to filter

    Return Value

    a Set of pending DynamicObjects of the specified type that were inserted to the transaction.

  • Returns all pending NSManagedObjectIDs that were inserted to the transaction. This method should not be called after the commit() method was called.

    Declaration

    Swift

    public func insertedObjectIDs() -> Set<NSManagedObjectID>

    Return Value

    a Set of pending NSManagedObjectIDs that were inserted to the transaction.

  • Returns all pending NSManagedObjectIDs of the specified type that were inserted to the transaction. This method should not be called after the commit() method was called.

    Declaration

    Swift

    public func insertedObjectIDs<D>(_ entity: D.Type) -> Set<NSManagedObjectID> where D : DynamicObject

    Parameters

    entity

    the DynamicObject subclass to filter

    Return Value

    a Set of pending NSManagedObjectIDs of the specified type that were inserted to the transaction.

  • Returns all pending DynamicObjects of the specified type that were updated in the transaction. This method should not be called after the commit() method was called.

    Declaration

    Swift

    public func updatedObjects<D>(_ entity: D.Type) -> Set<D> where D : DynamicObject, D : Hashable

    Parameters

    entity

    the DynamicObject subclass to filter

    Return Value

    a Set of pending DynamicObjects of the specified type that were updated in the transaction.

  • Returns all pending NSManagedObjectIDs that were updated in the transaction. This method should not be called after the commit() method was called.

    Declaration

    Swift

    public func updatedObjectIDs() -> Set<NSManagedObjectID>

    Return Value

    a Set of pending NSManagedObjectIDs that were updated in the transaction.

  • Returns all pending NSManagedObjectIDs of the specified type that were updated in the transaction. This method should not be called after the commit() method was called.

    Declaration

    Swift

    public func updatedObjectIDs<D>(_ entity: D.Type) -> Set<NSManagedObjectID> where D : DynamicObject

    Parameters

    entity

    the DynamicObject subclass to filter

    Return Value

    a Set of pending NSManagedObjectIDs of the specified type that were updated in the transaction.

  • Returns all pending DynamicObjects of the specified type that were deleted from the transaction. This method should not be called after the commit() method was called.

    Declaration

    Swift

    public func deletedObjects<D>(_ entity: D.Type) -> Set<D> where D : DynamicObject, D : Hashable

    Parameters

    entity

    the DynamicObject subclass to filter

    Return Value

    a Set of pending DynamicObjects of the specified type that were deleted from the transaction.

  • Returns all pending NSManagedObjectIDs of the specified type that were deleted from the transaction. This method should not be called after the commit() method was called.

    Declaration

    Swift

    public func deletedObjectIDs() -> Set<NSManagedObjectID>

    Parameters

    entity

    the DynamicObject subclass to filter

    Return Value

    a Set of pending NSManagedObjectIDs of the specified type that were deleted from the transaction.

  • Returns all pending NSManagedObjectIDs of the specified type that were deleted from the transaction. This method should not be called after the commit() method was called.

    Declaration

    Swift

    public func deletedObjectIDs<D>(_ entity: D.Type) -> Set<NSManagedObjectID> where D : DynamicObject

    Parameters

    entity

    the DynamicObject subclass to filter

    Return Value

    a Set of pending NSManagedObjectIDs of the specified type that were deleted from the transaction.

  • Allow external libraries to store custom data in the transaction. App code should rarely have a need for this.

    enum Static {
       static var myDataKey: Void?
    }
    transaction.userInfo[&Static.myDataKey] = myObject
    

    Important

    Do not use this method to store thread-sensitive data.

    Declaration

    Swift

    public let userInfo: UserInfo
  • Creates an ImportableObject by importing from the specified import source.

    Throws

    an Error thrown from any of the ImportableObject methods

    Declaration

    Swift

    public func importObject<D: ImportableObject>(
        _ into: Into<D>,
        source: D.ImportSource) throws -> D?

    Parameters

    into

    an Into clause specifying the entity type

    source

    the object to import values from

    Return Value

    the created ImportableObject instance, or nil if the import was ignored

  • Updates an existing ImportableObject by importing values from the specified import source.

    Throws

    an Error thrown from any of the ImportableObject methods

    Declaration

    Swift

    public func importObject<D: ImportableObject>(
        _ object: D,
        source: D.ImportSource) throws

    Parameters

    object

    the NSManagedObject to update

    source

    the object to import values from

  • Creates multiple ImportableObjects by importing from the specified array of import sources.

    Throws

    an Error thrown from any of the ImportableObject methods

    Declaration

    Swift

    public func importObjects<D: ImportableObject, S: Sequence>(
        _ into: Into<D>,
        sourceArray: S) throws -> [D] where S.Iterator.Element == D.ImportSource

    Parameters

    into

    an Into clause specifying the entity type

    sourceArray

    the array of objects to import values from

    Return Value

    the array of created ImportableObject instances

  • Updates an existing ImportableUniqueObject or creates a new instance by importing from the specified import source.

    Throws

    an Error thrown from any of the ImportableUniqueObject methods

    Declaration

    Swift

    public func importUniqueObject<D: ImportableUniqueObject>(
        _ into: Into<D>,
        source: D.ImportSource) throws -> D?

    Parameters

    into

    an Into clause specifying the entity type

    source

    the object to import values from

    Return Value

    the created/updated ImportableUniqueObject instance, or nil if the import was ignored

  • Updates existing ImportableUniqueObjects or creates them by importing from the specified array of import sources. ImportableUniqueObject methods are called on the objects in the same order as they are in the sourceArray, and are returned in an array with that same order.

    Warning

    If sourceArray contains multiple import sources with same ID, only the last ImportSource of the duplicates will be imported.

    Throws

    an Error thrown from any of the ImportableUniqueObject methods

    Declaration

    Swift

    public func importUniqueObjects<D: ImportableUniqueObject, S: Sequence>(
        _ into: Into<D>,
        sourceArray: S,
        preProcess: @escaping (_ mapping: [D.UniqueIDType: D.ImportSource]) throws -> [D.UniqueIDType: D.ImportSource] = { $0 }) throws -> [D] where S.Iterator.Element == D.ImportSource

    Parameters

    into

    an Into clause specifying the entity type

    sourceArray

    the array of objects to import values from

    preProcess

    a closure that lets the caller tweak the internal UniqueIDType-to-ImportSource mapping to be used for importing. Callers can remove from/add to/update mapping and return the updated array from the closure.

    Return Value

    the array of created/updated ImportableUniqueObject instances

  • Queries aggregate values as specified by the QueryClauses. Requires at least a Select clause, and optional Where, OrderBy, GroupBy, and Tweak clauses.

    A query differs from a fetch in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.

    Declaration

    Swift

    public func queryValue<D, U>(_ from: From<D>, _ selectClause: Select<D, U>, _ queryClauses: QueryClause...) -> U? where D : DynamicObject, U : QueryableAttributeType

    Parameters

    from

    a From clause indicating the entity type

    selectClause

    a Select<U> clause indicating the properties to fetch, and with the generic type indicating the return type.

    queryClauses

    a series of QueryClause instances for the query request. Accepts Where, OrderBy, GroupBy, and Tweak clauses.

    Return Value

    the result of the the query. The type of the return value is specified by the generic type of the Select<U> parameter.

  • Queries aggregate values or aggregates as specified by the QueryClauses. Requires at least a Select clause, and optional Where, OrderBy, GroupBy, and Tweak clauses.

    A query differs from a fetch in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.

    Declaration

    Swift

    public func queryValue<D, U>(_ from: From<D>, _ selectClause: Select<D, U>, _ queryClauses: [QueryClause]) -> U? where D : DynamicObject, U : QueryableAttributeType

    Parameters

    from

    a From clause indicating the entity type

    selectClause

    a Select<U> clause indicating the properties to fetch, and with the generic type indicating the return type.

    queryClauses

    a series of QueryClause instances for the query request. Accepts Where, OrderBy, GroupBy, and Tweak clauses.

    Return Value

    the result of the the query. The type of the return value is specified by the generic type of the Select<U> parameter.

  • Queries a property value or aggregate as specified by the QueryChainableBuilderType built from a chain of clauses.

    A query differs from a fetch in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.

    let averageAdultAge = transaction.queryValue(
        From<MyPersonEntity>()
            .select(Int.self, .average(\.age))
            .where(\.age > 18)
    )
    

    Declaration

    Swift

    public func queryValue<B>(_ clauseChain: B) -> B.ResultType? where B : QueryChainableBuilderType, B.ResultType : QueryableAttributeType

    Parameters

    clauseChain

    a QueryChainableBuilderType indicating the property/aggregate to fetch and the series of queries for the request.

    Return Value

    the result of the the query as specified by the QueryChainableBuilderType

  • Queries a dictionary of attribute values as specified by the QueryClauses. Requires at least a Select clause, and optional Where, OrderBy, GroupBy, and Tweak clauses.

    A query differs from a fetch in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.

    Declaration

    Swift

    public func queryAttributes<D>(_ from: From<D>, _ selectClause: Select<D, NSDictionary>, _ queryClauses: QueryClause...) -> [[String : Any]]? where D : DynamicObject

    Parameters

    from

    a From clause indicating the entity type

    selectClause

    a Select<U> clause indicating the properties to fetch, and with the generic type indicating the return type.

    queryClauses

    a series of QueryClause instances for the query request. Accepts Where, OrderBy, GroupBy, and Tweak clauses.

    Return Value

    the result of the the query. The type of the return value is specified by the generic type of the Select<U> parameter.

  • Queries a dictionary of attribute values as specified by the QueryClauses. Requires at least a Select clause, and optional Where, OrderBy, GroupBy, and Tweak clauses.

    A query differs from a fetch in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.

    Declaration

    Swift

    public func queryAttributes<D>(_ from: From<D>, _ selectClause: Select<D, NSDictionary>, _ queryClauses: [QueryClause]) -> [[String : Any]]? where D : DynamicObject

    Parameters

    from

    a From clause indicating the entity type

    selectClause

    a Select<U> clause indicating the properties to fetch, and with the generic type indicating the return type.

    queryClauses

    a series of QueryClause instances for the query request. Accepts Where, OrderBy, GroupBy, and Tweak clauses.

    Return Value

    the result of the the query. The type of the return value is specified by the generic type of the Select<U> parameter.

  • Queries a dictionary of attribute values or as specified by the QueryChainableBuilderType built from a chain of clauses.

    A query differs from a fetch in that it only retrieves values already stored in the persistent store. As such, values from unsaved transactions or contexts will not be incorporated in the query result.

    let results = dataStack.queryAttributes(
        From<MyPersonEntity>()
            .select(
                NSDictionary.self,
                .attribute(\.age, as: "age"),
                .count(\.age, as: "numberOfPeople")
             )
            .groupBy(\.age)
    )
    for dictionary in results! {
        let age = dictionary["age"] as! Int
        let count = dictionary["numberOfPeople"] as! Int
        print("There are \(count) people who are \(age) years old."
    }
    

    Declaration

    Swift

    public func queryAttributes<B>(_ clauseChain: B) -> [[String : Any]]? where B : QueryChainableBuilderType, B.ResultType == NSDictionary

    Parameters

    clauseChain

    a QueryChainableBuilderType indicating the properties to fetch and the series of queries for the request.

    Return Value

    the result of the the query as specified by the QueryChainableBuilderType

  • The internal NSManagedObjectContext managed by this instance. Using this context directly should typically be avoided, and is provided by CoreStore only for extremely specialized cases.

    Declaration

    Swift

    public func unsafeContext() -> NSManagedObjectContext