Classes
The following classes are available globally.
-
The
See moreAsynchronousDataTransaction
provides an interface forDynamicObject
creates, updates, and deletes. A transaction object should typically be only used from within a transaction block initiated fromDataStack.perform(asynchronous:...)
.Declaration
Swift
public final class AsynchronousDataTransaction : BaseDataTransaction
extension AsynchronousDataTransaction: CustomDebugStringConvertible, CoreStoreDebugStringConvertible
-
The
See moreBaseDataTransaction
is an abstract interface forNSManagedObject
creates, updates, and deletes. AllBaseDataTransaction
subclasses manage a privateNSManagedObjectContext
which are direct children of theNSPersistentStoreCoordinator
‘s rootNSManagedObjectContext
. This means that all updates are saved first to the persistent store, and then propagated up to the read-onlyNSManagedObjectContext
.Declaration
Swift
public class BaseDataTransaction
extension BaseDataTransaction: FetchableSource, QueryableSource
-
The
CoreStoreObject
is an abstract class for creating CoreStore-managed objects that are more type-safe and more convenient thanNSManagedObject
subclasses. The model entities forCoreStoreObject
subclasses are inferred from the Swift declaration themselves; no .xcdatamodeld files are needed. To declare persisted attributes and relationships for theCoreStoreObject
subclass, declare properties of typeValue.Required<T>
,Value.Optional<T>
for values, orRelationship.ToOne<T>
,Relationship.ToManyOrdered<T>
,Relationship.ToManyUnordered<T>
for relationships.class Animal: CoreStoreObject { let species = Value.Required<String>("species", initial: "") let nickname = Value.Optional<String>("nickname") let master = Relationship.ToOne<Person>("master") } class Person: CoreStoreObject { let name = Value.Required<String>("name", initial: "") let pet = Relationship.ToOne<Animal>("pet", inverse: { $0.master }) }
CoreStoreObject
entities for a model version should be added toCoreStoreSchema
instance.CoreStoreDefaults.dataStack = DataStack( CoreStoreSchema( modelVersion: "V1", entities: [ Entity<Animal>("Animal"), Entity<Person>("Person") ] ) )
See also
CoreStoreSchemaSee also
CoreStoreObject.ValueSee also
CoreStoreObject.RelationshipDeclaration
Swift
open class CoreStoreObject : DynamicObject, Hashable
extension CoreStoreObject: CustomDebugStringConvertible, CoreStoreDebugStringConvertible
extension CoreStoreObject: ObservableObject
extension CoreStoreObject: ObjectRepresentation
-
The
CoreStoreSchema
describes models written forCoreStoreObject
Swift class declarations for a particular model version.CoreStoreObject
entities for a model version should be added toCoreStoreSchema
instance.class Animal: CoreStoreObject { let species = Value.Required<String>("species", initial: "") let nickname = Value.Optional<String>("nickname") let master = Relationship.ToOne<Person>("master") } class Person: CoreStoreObject { let name = Value.Required<String>("name", initial: "") let pet = Relationship.ToOne<Animal>("pet", inverse: { $0.master }) } CoreStoreDefaults.dataStack = DataStack( CoreStoreSchema( modelVersion: "V1", entities: [ Entity<Animal>("Animal"), Entity<Person>("Person") ], versionLock: [ "Animal": [0x2698c812ebbc3b97, 0x751e3fa3f04cf9, 0x51fd460d3babc82, 0x92b4ba735b5a3053], "Person": [0xae4060a59f990ef0, 0x8ac83a6e1411c130, 0xa29fea58e2e38ab6, 0x2071bb7e33d77887] ] ) )
See also
CoreStoreObjectSee also
EntityDeclaration
Swift
public final class CoreStoreSchema : DynamicSchema
extension CoreStoreSchema: CustomDebugStringConvertible, CoreStoreDebugStringConvertible
-
The
See moreDataStack
encapsulates the data model for the Core Data stack. EachDataStack
can have multiple data stores, usually specified as a “Configuration” in the model editor. Behind the scenes, the DataStack manages its ownNSPersistentStoreCoordinator
, a rootNSManagedObjectContext
for disk saves, and a sharedNSManagedObjectContext
designed as a read-only model interface forNSManagedObjects
.Declaration
Swift
public final class DataStack : Equatable
extension DataStack: CustomDebugStringConvertible, CoreStoreDebugStringConvertible
extension DataStack: FetchableSource, QueryableSource
-
The
Entity<O>
containsNSEntityDescription
metadata forCoreStoreObject
subclasses. Pass theEntity
instances toCoreStoreSchema
initializer.class Animal: CoreStoreObject { let species = Value.Required<String>("species", initial: "") let nickname = Value.Optional<String>("nickname") let master = Relationship.ToOne<Person>("master") } class Person: CoreStoreObject { let name = Value.Required<String>("name", initial: "") let pet = Relationship.ToOne<Animal>("pet", inverse: { $0.master }) } CoreStoreDefaults.dataStack = DataStack( CoreStoreSchema( modelVersion: "V1", entities: [ Entity<Animal>("Animal"), Entity<Person>("Person") ] ) )
See also
CoreStoreSchemaSee also
CoreStoreObjectDeclaration
Swift
public final class Entity<O> : DynamicEntity where O : CoreStoreObject
extension Entity: CustomDebugStringConvertible, CoreStoreDebugStringConvertible
-
A storage interface that is backed only in memory.
See moreDeclaration
Swift
public final class InMemoryStore : StorageInterface
extension InMemoryStore: CustomDebugStringConvertible, CoreStoreDebugStringConvertible
-
The
See moreUnsafeDataModelSchema
describes models loaded directly from an existingNSManagedObjectModel
. It is not advisable to continue using this model as its metadata are not available to CoreStore.Declaration
Swift
public final class UnsafeDataModelSchema : DynamicSchema
extension UnsafeDataModelSchema: CustomDebugStringConvertible, CoreStoreDebugStringConvertible
-
The
ListMonitor
monitors changes to a list ofDynamicObject
instances. Observers that implement theListObserver
protocol may then register themselves to theListMonitor
‘saddObserver(_:)
method:let monitor = dataStack.monitorList( From<Person>(), Where("title", isEqualTo: "Engineer"), OrderBy(.ascending("lastName")) ) monitor.addObserver(self)
The
ListMonitor
instance needs to be held on (retained) for as long as the list needs to be observed. Observers registered viaaddObserver(_:)
are not retained.ListMonitor
only keeps aweak
reference to all observers, thus keeping itself free from retain-cycles.Lists created with
monitorList(...)
keep a single-section list of objects, where each object can be accessed by index:let firstPerson: MyPersonEntity = monitor[0]
Accessing the list with an index above the valid range will raise an exception.
Creating a sectioned-list is also possible with the
monitorSectionedList(...)
method:let monitor = dataStack.monitorSectionedList( From<Person>(), SectionBy("age") { "Age \($0)" }, Where("title", isEqualTo: "Engineer"), OrderBy(.ascending("lastName")) ) monitor.addObserver(self)
Objects from
ListMonitor
s created this way can be accessed either by anIndexPath
or a tuple:let indexPath = IndexPath(forItem: 3, inSection: 2) let person1 = monitor[indexPath] let person2 = monitor[2, 3]
In the example above, both
See moreperson1
andperson2
will contain the object at section=2, index=3.Declaration
Swift
public final class ListMonitor<O> : Hashable where O : DynamicObject
extension ListMonitor: CustomDebugStringConvertible, CoreStoreDebugStringConvertible
-
ListPublisher
tracks a diffable list ofDynamicObject
instances. UnlikeListMonitor
s,ListPublisher
are more lightweight and access objects lazily. Objects that need to be notified ofListPublisher
changes may register themselves to itsaddObserver(_:_:)
method:let listPublisher = CoreStoreDefaults.dataStack.listPublisher( From<Person>() .where(\.title == "Engineer") .orderBy(.ascending(\.lastName)) ) listPublisher.addObserver(self) { (listPublisher) in // Handle changes }
The
ListPublisher
instance needs to be held on (retained) for as long as the list needs to be observed. Observers registered viaaddObserver(_:_:)
are not retained.ListPublisher
only keeps aweak
reference to all observers, thus keeping itself free from retain-cycles.ListPublisher
s may optionally be created with sections:let listPublisher = CoreStoreDefaults.dataStack.listPublisher( From<Person>() .sectionBy(\.age") { "Age \($0)" } .where(\.title == "Engineer") .orderBy(.ascending(\.lastName)) )
All access to the
See moreListPublisher
items should be done via itssnapshot
value, which is astruct
of typeListSnapshot<O>
.ListSnapshot
s are also designed to work well withDiffableDataSource.TableViewAdapter
s andDiffableDataSource.CollectionViewAdapter
s. For detailed examples, refer to the documentation forDiffableDataSource.TableViewAdapter
andDiffableDataSource.CollectionViewAdapter
.Declaration
Swift
public final class ListPublisher<O> : Hashable where O : DynamicObject
extension ListPublisher: CustomDebugStringConvertible, CoreStoreDebugStringConvertible
-
The
ObjectMonitor
monitors changes to a singleDynamicObject
instance. Observers that implement theObjectObserver
protocol may then register themselves to theObjectMonitor
‘saddObserver(_:)
method:let monitor = dataStack.monitorObject(object) monitor.addObserver(self)
The created
ObjectMonitor
instance needs to be held on (retained) for as long as the object needs to be observed.Observers registered via
See moreaddObserver(_:)
are not retained.ObjectMonitor
only keeps aweak
reference to all observers, thus keeping itself free from retain-cycles.Declaration
Swift
public final class ObjectMonitor<O> : Hashable, ObjectRepresentation where O : DynamicObject
extension ObjectMonitor: CustomDebugStringConvertible, CoreStoreDebugStringConvertible
-
The
ObjectPublisher
tracks changes to a singleDynamicObject
instance. Objects that need to be notified ofObjectPublisher
changes may register themselves to itsaddObserver(_:_:)
method:let objectPublisher = CoreStoreDefaults.dataStack.objectPublisher(object) objectPublisher.addObserver(self) { (objectPublisher) in // Handle changes }
The created
ObjectPublisher
instance needs to be held on (retained) for as long as the object needs to be observed.Observers registered via
addObserver(_:_:)
are not retained.ObjectPublisher
only keeps aweak
reference to all observers, thus keeping itself free from retain-cycles.The
See moreObjectPublisher
‘ssnapshot
value is a lazy copy operation that extracts all property values at a specific point time. This cached copy is invalidated right before theObjectPublisher
broadcasts any changes to its observers.Declaration
Swift
@dynamicMemberLookup public final class ObjectPublisher<O> : ObjectRepresentation, Hashable where O : DynamicObject
extension ObjectPublisher: CustomDebugStringConvertible, CoreStoreDebugStringConvertible
-
The
See moreSchemaHistory
encapsulates multipleDynamicSchema
across multiple model versions. It contains all model history and is used by theDataStack
toDeclaration
Swift
public final class SchemaHistory : ExpressibleByArrayLiteral
extension SchemaHistory: CustomDebugStringConvertible, CoreStoreDebugStringConvertible
-
A storage interface that is backed by an SQLite database.
Warning
The default SQLite file location for theLegacySQLiteStore
andSQLiteStore
are different. If the app was depending on CoreStore’s default directories prior to 2.0.0, make sure to use theSQLiteStore.legacy(...)
factory methods to create theSQLiteStore
instead of using initializers directly.Declaration
Swift
public final class SQLiteStore : LocalStorage
extension SQLiteStore: CustomDebugStringConvertible, CoreStoreDebugStringConvertible
-
The
See moreSynchronousDataTransaction
provides an interface forDynamicObject
creates, updates, and deletes. A transaction object should typically be only used from within a transaction block initiated fromDataStack.beginSynchronous(_:)
.Declaration
Swift
public final class SynchronousDataTransaction : BaseDataTransaction
extension SynchronousDataTransaction: CustomDebugStringConvertible, CoreStoreDebugStringConvertible
-
The
See moreUnsafeDataTransaction
provides an interface for non-contiguousNSManagedObject
orCoreStoreObject
creates, updates, and deletes. This is useful for making temporary changes, such as partially filled forms. An unsafe transaction object should typically be only used from the main queue.Declaration
Swift
public final class UnsafeDataTransaction : BaseDataTransaction
extension UnsafeDataTransaction: CustomDebugStringConvertible, CoreStoreDebugStringConvertible
-
The
XcodeDataModelSchema
describes a model version declared in a single *.xcdatamodeld file.
See moreCoreStoreDefaults.dataStack = DataStack( XcodeDataModelSchema(modelName: "MyAppV1", bundle: .main) )
Declaration
Swift
public final class XcodeDataModelSchema : DynamicSchema
extension XcodeDataModelSchema: CustomDebugStringConvertible, CoreStoreDebugStringConvertible
-
The object containing the changeset for an observed
See moreValueContainer.Required
andValueContainer.Optional
property.Declaration
Swift
public final class CoreStoreObjectValueDiff<V> where V : ImportableAttributeType
-
The object containing the changeset for an observed
See moreTransformableContainer.Required
orTransformableContainer.Optional
property.Declaration
Swift
public final class CoreStoreObjectTransformableDiff<V> where V : NSCoding, V : NSCopying
-
The object containing the changeset for an observed
See moreRelationshipContainer.ToOne
property.Declaration
Swift
public final class CoreStoreObjectObjectDiff<D> where D : CoreStoreObject
-
The object containing the changeset for an observed
See moreRelationshipContainer.ToManyUnordered
property.Declaration
Swift
public final class CoreStoreObjectUnorderedDiff<D> where D : CoreStoreObject
-
The object containing the changeset for an observed
See moreRelationshipContainer.Ordered
property.Declaration
Swift
public final class CoreStoreObjectOrderedDiff<D> where D : CoreStoreObject
-
A
See moreSchemaMappingProvider
that accepts custom mappings for some entities. Mappings of entities with noCustomMapping
provided will be automatically calculated if possible.Declaration
Swift
public class CustomSchemaMappingProvider : Hashable, SchemaMappingProvider
-
The
See moreDefaultLogger
is a basic implementation of theCoreStoreLogger
protocol.Declaration
Swift
public final class DefaultLogger : CoreStoreLogger
-
A
SchemaMappingProvider
that tries to infer model migration between twoDynamicSchema
versions by searching allxcmappingmodel
s fromBundle.allBundles
or by relying on lightweight migration if possible. Throws an error if lightweight migration is impossible for the twoDynamicSchema
. This mapping is automatically used as a fallback mapping provider, even if no mapping providers are explicitly declared in theStorageInterface
.Note
For security reasons,InferredSchemaMappingProvider
will not searchBundle.allFrameworks
by default. If thexcmappingmodel
s are bundled within a framework, useXcodeSchemaMappingProvider
instead and provideBundle(for: <a class in the framework>
to its initializer.Declaration
Swift
public final class InferredSchemaMappingProvider : Hashable, SchemaMappingProvider
-
The
UserInfo
class is provided by several CoreStore types such asDataStack
,ListMonitor
,ObjectMonitor
and transactions to allow external libraries or user apps to store their own custom data.enum Static { static var myDataKey: Void? } CoreStoreDefaults.dataStack.userInfo[&Static.myDataKey] = myObject
Important
Do not use this class to store thread-sensitive data.Declaration
Swift
public final class UserInfo
-
A
See moreSchemaMappingProvider
that tries to infer model migration between twoDynamicSchema
versions by loading an xcmappingmodel file from the specifiedBundle
. ThrowsCoreStoreError.mappingModelNotFound
if the xcmappingmodel file cannot be found, or if the xcmappingmodel doesn’t resolve the source and destinationDynamicSchema
.Declaration
Swift
public final class XcodeSchemaMappingProvider : Hashable, SchemaMappingProvider