CSDataStack
@objc
public final class CSDataStack : NSObject, CoreStoreObjectiveCType
The CSDataStack serves as the Objective-C bridging type for DataStack.
See also
DataStack
-
Initializes a
CSDataStackwith default settings. CoreStore searches for.xcdatamodeld from the main NSBundleand loads anNSManagedObjectModelfrom it. An assertion is raised if the model could not be found.Declaration
Swift
@objc public convenience override init() -
Initializes a
CSDataStackfrom the model with the specifiedmodelNamein the specifiedbundle.Declaration
Swift
@objc public convenience init(xcodeModelName: XcodeDataModelFileName?, bundle: Bundle?, versionChain: [String]?)Parameters
xcodeModelNamethe name of the (.xcdatamodeld) model file. If not specified, the application name (CFBundleName) will be used if it exists, or
CoreData
if it the bundle name was not set.bundlean optional bundle to load .xcdatamodeld models from. If not specified, the main bundle will be used.
versionChainthe version strings that indicate the sequence of model versions to be used as the order for progressive migrations. If not specified, will default to a non-migrating data stack.
-
Returns the stack’s model version. The version string is the same as the name of the version-specific .xcdatamodeld file.
Declaration
Swift
@objc public var modelVersion: String { get } -
Returns the entity name-to-class type mapping from the
CSDataStack‘s model.Declaration
Swift
@objc public func entityTypesByNameForType(_ type: NSManagedObject.Type) -> [EntityName : NSManagedObject.Type] -
Returns the
NSEntityDescriptionfor the specifiedNSManagedObjectsubclass from stack’s model.Declaration
Swift
@objc public func entityDescriptionForClass(_ type: NSManagedObject.Type) -> NSEntityDescription? -
Creates an
CSInMemoryStorewith default parameters and adds it to the stack. This method blocks until completion.CSSQLiteStore *storage = [dataStack addInMemoryStorageAndWaitAndReturnError:&error];Declaration
Swift
@discardableResult @objc public func addInMemoryStorageAndWaitAndReturnError(_ error: NSErrorPointer) -> CSInMemoryStore?Parameters
errorthe
NSErrorpointer that indicates the reason in case of an failureReturn Value
the
CSInMemoryStoreadded to the stack -
Creates an
CSSQLiteStorewith default parameters and adds it to the stack. This method blocks until completion.CSSQLiteStore *storage = [dataStack addSQLiteStorageAndWaitAndReturnError:&error];Declaration
Swift
@discardableResult @objc public func addSQLiteStorageAndWaitAndReturnError(_ error: NSErrorPointer) -> CSSQLiteStore?Parameters
errorthe
NSErrorpointer that indicates the reason in case of an failureReturn Value
the
CSSQLiteStoreadded to the stack -
Adds a
CSInMemoryStoreto the stack and blocks until completion.NSError *error; CSInMemoryStore *storage = [dataStack addStorageAndWait: [[CSInMemoryStore alloc] initWithConfiguration: @"Config1"] error: &error];Declaration
Swift
@discardableResult @objc public func addInMemoryStorageAndWait(_ storage: CSInMemoryStore, error: NSErrorPointer) -> CSInMemoryStore?Parameters
storagethe
CSInMemoryStoreerrorthe
NSErrorpointer that indicates the reason in case of an failureReturn Value
the
CSInMemoryStoreadded to the stack -
Adds a
CSSQLiteStoreto the stack and blocks until completion.NSError *error; CSSQLiteStore *storage = [dataStack addStorageAndWait: [[CSSQLiteStore alloc] initWithConfiguration: @"Config1"] error: &error];Declaration
Swift
@discardableResult @objc public func addSQLiteStorageAndWait(_ storage: CSSQLiteStore, error: NSErrorPointer) -> CSSQLiteStore?Parameters
storagethe
CSSQLiteStoreerrorthe
NSErrorpointer that indicates the reason in case of an failureReturn Value
the
CSSQLiteStoreadded to the stack
-
Asynchronously adds a
CSInMemoryStoreto the stack. Migrations are also initiated by default.NSError *error; NSProgress *migrationProgress = [dataStack addInMemoryStorage:[CSInMemoryStore new] completion:^(CSSetupResult *result) { if (result.isSuccess) { // ... } } error: &error];Declaration
Swift
@objc public func addInMemoryStorage(_ storage: CSInMemoryStore, completion: @escaping (CSSetupResult) -> Void)Parameters
storagethe
CSInMemoryStoreinstancecompletionthe closure to be executed on the main queue when the process completes, either due to success or failure. The closure’s
CSSetupResultargument indicates the result. This closure is NOT executed if an error is thrown, but will be executed with a failureCSSetupResultresult if an error occurs asynchronously. -
Asynchronously adds a
CSSQLiteStoreto the stack. Migrations are also initiated by default.NSError *error; NSProgress *migrationProgress = [dataStack addInMemoryStorage:[[CSSQLiteStore alloc] initWithFileName:@"core_data.sqlite" configuration:@"Config1"] completion:^(CSSetupResult *result) { if (result.isSuccess) { // ... } } error: &error];Declaration
Swift
@objc public func addSQLiteStorage(_ storage: CSSQLiteStore, completion: @escaping (CSSetupResult) -> Void, error: NSErrorPointer) -> Progress?Parameters
storagethe
CSSQLiteStoreinstancecompletionthe closure to be executed on the main queue when the process completes, either due to success or failure. The closure’s
CSSetupResultargument indicates the result. This closure is NOT executed if an error is thrown, but will be executed with a failureCSSetupResultresult if an error occurs asynchronously. Note that theCSLocalStorageassociated to the-[CSSetupResult storage]may not always be the same instance as the parameter argument if a previousCSLocalStoragewas already added at the same URL and with the same configuration.errorthe
NSErrorpointer that indicates the reason in case of an failureReturn Value
an
NSProgressinstance if a migration has started.nilif no migrations are required or iferrorwas set. -
Migrates a
CSSQLiteStoreto match theCSDataStack‘s managed object model version. This method does NOT add the migrated store to the data stack.Declaration
Swift
@objc public func upgradeStorageIfNeeded(_ storage: CSSQLiteStore, completion: @escaping (CSMigrationResult) -> Void, error: NSErrorPointer) -> Progress?Parameters
storagethe
CSSQLiteStoreinstancecompletionthe closure to be executed on the main queue when the migration completes, either due to success or failure. The closure’s
CSMigrationResultargument indicates the result. This closure is NOT executed if an error is thrown, but will be executed with a failureCSSetupResultresult if an error occurs asynchronously.errorthe
NSErrorpointer that indicates the reason in case of an failureReturn Value
an
NSProgressinstance if a migration has started.nilif no migrations are required or iferrorwas set. -
Checks the migration steps required for the
CSSQLiteStoreto match theCSDataStack‘s managed object model version.Declaration
Swift
@objc public func requiredMigrationsForSQLiteStore(_ storage: CSSQLiteStore, error: NSErrorPointer) -> [CSMigrationType]?Parameters
storagethe
CSSQLiteStoreinstanceerrorthe
NSErrorpointer that indicates the reason in case of an failureReturn Value
a
CSMigrationTypearray indicating the migration steps required for the store, or an empty array if the file does not exist yet. Otherwise,nilis returned and theerrorargument is set if either inspection of the store failed, or if no mapping model was found/inferred.
-
Creates a
CSObjectMonitorfor the specifiedNSManagedObject. MultipleObjectObservers may then register themselves to be notified when changes are made to theNSManagedObject.Declaration
Swift
@objc public func monitorObject(_ object: NSManagedObject) -> CSObjectMonitorParameters
objectthe
NSManagedObjectto observe changes fromReturn Value
a
ObjectMonitorthat monitors changes toobject -
Creates a
CSListMonitorfor a list ofNSManagedObjects that satisfy the specified fetch clauses. MultipleCSListObservers may then register themselves to be notified when changes are made to the list.Declaration
Swift
@objc public func monitorListFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> CSListMonitorParameters
froma
CSFromclause indicating the entity typefetchClausesa series of
FetchClauseinstances for fetching the object list. AcceptsCSWhere,CSOrderBy, andCSTweakclauses.Return Value
a
CSListMonitorinstance that monitors changes to the list -
Asynchronously creates a
CSListMonitorfor a list ofNSManagedObjects that satisfy the specified fetch clauses. MultipleCSListObservers may then register themselves to be notified when changes are made to the list. SinceNSFetchedResultsControllergreedily locks the persistent store on initial fetch, you may prefer this method instead of the synchronous counterpart to avoid deadlocks while background updates/saves are being executed.Declaration
Swift
@objc public func monitorListByCreatingAsynchronously(_ createAsynchronously: @escaping (CSListMonitor) -> Void, from: CSFrom, fetchClauses: [CSFetchClause])Parameters
createAsynchronouslythe closure that receives the created
CSListMonitorinstancefroma
CSFromclause indicating the entity typefetchClausesa series of
CSFetchClauseinstances for fetching the object list. AcceptsCSWhere,CSOrderBy, andCSTweakclauses. -
Creates a
CSListMonitorfor a sectioned list ofNSManagedObjects that satisfy the specified fetch clauses. MultipleListObservers may then register themselves to be notified when changes are made to the list.Declaration
Swift
@objc public func monitorSectionedListFrom(_ from: CSFrom, sectionBy: CSSectionBy, fetchClauses: [CSFetchClause]) -> CSListMonitorParameters
froma
CSFromclause indicating the entity typesectionBya
CSSectionByclause indicating the keyPath for the attribute to use when sorting the list into sections.fetchClausesa series of
FetchClauseinstances for fetching the object list. AcceptsCSWhere,CSOrderBy, andCSTweakclauses.Return Value
a
CSListMonitorinstance that monitors changes to the list -
Asynchronously creates a
CSListMonitorfor a sectioned list ofNSManagedObjects that satisfy the specified fetch clauses. MultipleCSListObservers may then register themselves to be notified when changes are made to the list. SinceNSFetchedResultsControllergreedily locks the persistent store on initial fetch, you may prefer this method instead of the synchronous counterpart to avoid deadlocks while background updates/saves are being executed.Declaration
Swift
public func monitorSectionedListByCreatingAsynchronously(_ createAsynchronously: @escaping (CSListMonitor) -> Void, from: CSFrom, sectionBy: CSSectionBy, fetchClauses: [CSFetchClause])Parameters
createAsynchronouslythe closure that receives the created
CSListMonitorinstancefroma
CSFromclause indicating the entity typesectionBya
CSSectionByclause indicating the keyPath for the attribute to use when sorting the list into sections.fetchClausesa series of
CSFetchClauseinstances for fetching the object list. AcceptsCSWhere,CSOrderBy, andCSTweakclauses.
-
Fetches the
NSManagedObjectinstance in the transaction’s context from a reference created from a transaction or from a different managed object context.Declaration
Swift
@objc public func fetchExistingObject(_ object: NSManagedObject) -> Any?Parameters
objecta reference to the object created/fetched outside the transaction
Return Value
the
NSManagedObjectinstance if the object exists in the transaction, ornilif not found. -
Fetches the
NSManagedObjectinstance in the transaction’s context from anNSManagedObjectID.Declaration
Swift
@objc public func fetchExistingObjectWithID(_ objectID: NSManagedObjectID) -> Any?Parameters
objectIDthe
NSManagedObjectIDfor the objectReturn Value
the
NSManagedObjectinstance if the object exists in the transaction, ornilif not found. -
Fetches the
NSManagedObjectinstances in the transaction’s context from references created from a transaction or from a different managed object context.Declaration
Swift
@objc public func fetchExistingObjects(_ objects: [NSManagedObject]) -> [Any]Parameters
objectsan array of
NSManagedObjects created/fetched outside the transactionReturn Value
the
NSManagedObjectarray for objects that exists in the transaction -
Fetches the
NSManagedObjectinstances in the transaction’s context from a list ofNSManagedObjectID.Declaration
Swift
@objc public func fetchExistingObjectsWithIDs(_ objectIDs: [NSManagedObjectID]) -> [Any]Parameters
objectIDsthe
NSManagedObjectIDarray for the objectsReturn Value
the
NSManagedObjectarray for objects that exists in the transaction -
Fetches the first
NSManagedObjectinstance that satisfies the specifiedCSFetchClauses. AcceptsCSWhere,CSOrderBy, andCSTweakclauses.Declaration
Swift
@objc public func fetchOneFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> Any?Parameters
froma
Fromclause indicating the entity typefetchClausesa series of
CSFetchClauseinstances for the fetch request. AcceptsCSWhere,CSOrderBy, andCSTweakclauses.Return Value
the first
NSManagedObjectinstance that satisfies the specifiedCSFetchClauses -
Fetches all
NSManagedObjectinstances that satisfy the specifiedCSFetchClauses. AcceptsCSWhere,CSOrderBy, andCSTweakclauses.Declaration
Swift
@objc public func fetchAllFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> [Any]?Parameters
froma
CSFromclause indicating the entity typefetchClausesa series of
CSFetchClauseinstances for the fetch request. AcceptsCSWhere,CSOrderBy, andCSTweakclauses.Return Value
all
NSManagedObjectinstances that satisfy the specifiedCSFetchClauses -
Fetches the number of
NSManagedObjects that satisfy the specifiedCSFetchClauses. AcceptsCSWhere,CSOrderBy, andCSTweakclauses.Declaration
Swift
@objc public func fetchCountFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> NSNumber?Parameters
froma
CSFromclause indicating the entity typefetchClausesa series of
CSFetchClauseinstances for the fetch request. AcceptsCSWhere,CSOrderBy, andCSTweakclauses.Return Value
the number
NSManagedObjects that satisfy the specifiedCSFetchClauses -
Fetches the
NSManagedObjectIDfor the firstNSManagedObjectthat satisfies the specifiedCSFetchClauses. AcceptsCSWhere,CSOrderBy, andCSTweakclauses.Declaration
Swift
@objc public func fetchObjectIDFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> NSManagedObjectID?Parameters
froma
CSFromclause indicating the entity typefetchClausesa series of
CSFetchClauseinstances for the fetch request. AcceptsCSWhere,CSOrderBy, andCSTweakclauses.Return Value
the
NSManagedObjectIDfor the firstNSManagedObjectthat satisfies the specifiedCSFetchClauses -
Fetches the
NSManagedObjectIDfor allNSManagedObjects that satisfy the specifiedCSFetchClauses. AcceptsCSWhere,CSOrderBy, andCSTweakclauses.Declaration
Swift
@objc public func fetchObjectIDsFrom(_ from: CSFrom, fetchClauses: [CSFetchClause]) -> [NSManagedObjectID]?Parameters
froma
CSFromclause indicating the entity typefetchClausesa series of
FetchClauseinstances for the fetch request. AcceptsCSWhere,CSOrderBy, andCSTweakclauses.Return Value
the
NSManagedObjectIDfor allNSManagedObjects that satisfy the specifiedCSFetchClauses -
Queries aggregate values as specified by the
CSQueryClauses. Requires at least aCSSelectclause, and optionalCSWhere,CSOrderBy,CSGroupBy, andCSTweakclauses.A
query
differs from afetch
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
@objc public func queryValueFrom(_ from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> Any?Parameters
froma
CSFromclause indicating the entity typeselectClausea
CSSelectclause indicating the properties to fetch, and with the generic type indicating the return type.queryClausesa series of
CSQueryClauseinstances for the query request. AcceptsCSWhere,CSOrderBy,CSGroupBy, andCSTweakclauses.Return Value
the result of the the query. The type of the return value is specified by the generic type of the
CSSelectparameter. -
Queries a dictionary of attribute values as specified by the
CSQueryClauses. Requires at least aCSSelectclause, and optionalCSWhere,CSOrderBy,CSGroupBy, andCSTweakclauses.A
query
differs from afetch
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
@objc public func queryAttributesFrom(_ from: CSFrom, selectClause: CSSelect, queryClauses: [CSQueryClause]) -> [[String : Any]]?Parameters
froma
CSFromclause indicating the entity typeselectClausea
CSSelectclause indicating the properties to fetch, and with the generic type indicating the return type.queryClausesa series of
CSQueryClauseinstances for the query request. AcceptsCSWhere,CSOrderBy,CSGroupBy, andCSTweakclauses.Return Value
the result of the the query. The type of the return value is specified by the generic type of the
CSSelectparameter.
-
Begins a transaction asynchronously where
NSManagedObjectcreates, updates, and deletes can be made.Declaration
Swift
@objc public func beginAsynchronous(_ closure: @escaping (_ transaction: CSAsynchronousDataTransaction) -> Void)Parameters
closurethe block where creates, updates, and deletes can be made to the transaction. Transaction blocks are executed serially in a background queue, and all changes are made from a concurrent
NSManagedObjectContext. -
Begins a transaction synchronously where
NSManagedObjectcreates, updates, and deletes can be made.Declaration
Swift
@objc public func beginSynchronous(_ closure: @escaping (_ transaction: CSSynchronousDataTransaction) -> Void, error: NSErrorPointer) -> BoolParameters
closurethe block where creates, updates, and deletes can be made to the transaction. Transaction blocks are executed serially in a background queue, and all changes are made from a concurrent
NSManagedObjectContext.errorthe
CSErrorpointer that indicates the reason in case of an failureReturn Value
YESif the commit succeeded,NOif the commit failed. IfNO, theerrorargument will hold error information. -
Begins a child transaction where
NSManagedObjectcreates, updates, and deletes can be made. This is useful for making temporary changes, such as partially filled forms.To support
undo
methods such as-undo,-redo, and-rollback, use the-beginSafeWithSupportsUndo:method passingYESto the argument. Withoutundo
support, calling those methods will raise an exception.Declaration
Swift
@objc public func beginUnsafe() -> CSUnsafeDataTransactionReturn Value
a
CSUnsafeDataTransactioninstance where creates, updates, and deletes can be made. -
Begins a child transaction where
NSManagedObjectcreates, updates, and deletes can be made. This is useful for making temporary changes, such as partially filled forms.- prameter supportsUndo:
-undo,-redo, and-rollbackmethods are only available when this parameter isYES, otherwise those method will raise an exception. Note that turning on Undo support may heavily impact performance especially on iOS or watchOS where memory is limited.
Declaration
Swift
@objc public func beginUnsafeWithSupportsUndo(_ supportsUndo: Bool) -> CSUnsafeDataTransactionReturn Value
a
CSUnsafeDataTransactioninstance where creates, updates, and deletes can be made. - prameter supportsUndo:
-
Refreshes all registered objects
NSManagedObjects in theDataStack.Declaration
Swift
@objc public func refreshAndMergeAllObjects()
View on GitHub
CSDataStack Class Reference