Store

class Store : NSObject

On-disk store of the boxes for your object types.

In every app, you have to setup a Store only once and call register(entity:) for each entity type to set the store up. Afterwards, you can obtain Box instances with the box(for:) methods. Boxes provide the interfaces for object persistence.

The code generator will create a convenience initializer for you to use, with sensible defaults set: Store.init(directoryPath:).

A typical setup sequence looks like this:

let store = try Store(directoryPath: pathToStoreData)
store.register(entity: Person.self)
let personBox = store.box(for: Person.self)
let persons = personBox.all()
  • A type that’s marked as an Entity and provides its own metadata.

    Declaration

    Swift

    public typealias InspectableEntity = Entity & EntityInspectable & __EntityRelatable
  • The Version of the ObjectBox library.

    Declaration

    Swift

    public var version: String { get }
  • The path of the database directory used by the store.

    Declaration

    Swift

    public var directoryPath: String { get }
  • Info that can be useful for debugging.

    Declaration

    Swift

    public var diagnostics: String { get }
  • Register an entity type for use with the database.

    You have to call this for every entity type once after Store setup and before you try to use any variant of box(for:).

    Declaration

    Swift

    public func register<T>(entity: T.Type) where T : Entity, T : EntityInspectable, T : __EntityRelatable

    Parameters

    entity

    The type you want to register.

  • Obtain a a Box for the given type.

    Note

    You need to register types first in register(entity:) or else you will get a runtime exception.

    Declaration

    Swift

    public func box<T>(for entityType: T.Type) -> Box<T> where T : Entity, T : EntityInspectable, T : __EntityRelatable

    Parameters

    entityType

    Registered object type to get a box for.

    Return Value

    Box for the given type.

  • Obtain a Box for the given relation’s target type.

    Note

    You need to register types first in register(entity:) or else you will get a runtime exception.

    Declaration

    Swift

    public func box<T>(for relation: ToOne<T>) -> Box<T> where T : Entity, T : EntityInspectable, T : __EntityRelatable

    Parameters

    relation

    ToOne relation for a registered object type.

    Return Value

    Box for the relation’s target type.

  • Runs the given block inside a transaction.

    Takes care of flattening nested transaction calls into a single transaction, and rolling back changes for you on error. You can e.g. wrap multiple put calls into a single write transaction to increase performance by re-using the resources of a single transaction to read and write data.

    You can nest read-only transaction into read/write transactions, but not vice versa.

    Throws

    NSError with OBXErrorDomain when an action inside the block throws.

    Declaration

    Swift

    public func runInTransaction<T>(_ block: () throws -> T) throws -> T

    Parameters

    block

    Code that needs to run in a read/write transaction.

    Return Value

    The forwarded result of block.

  • Runs the given block inside a transaction.

    Takes care of flattening nested transaction calls into a single transaction, and rolling back changes for you on error. You can e.g. wrap multiple put calls into a single write transaction to increase performance by re-using the resources of a single transaction to read and write data.

    You can nest read-only transaction into read/write transactions, but not vice versa.

    Throws

    NSError with OBXErrorDomain when an action inside the block throws.

    Declaration

    Swift

    public func runInTransaction(_ block: () throws -> Void) throws

    Parameters

    block

    Code that needs to run in a read/write transaction.

  • Runs the given block inside a transaction.

    Takes care of flattening nested transaction calls into a single transaction, and rolling back changes for you on error. You can e.g. wrap multiple put calls into a single write transaction to increase performance by re-using the resources of a single transaction to read and write data.

    You can nest read-only transaction into read/write transactions, but not vice versa.

    Declaration

    Swift

    public func runInTransaction<T>(_ block: () -> T) -> T

    Parameters

    block

    Code that needs to run in a read/write transaction.

    Return Value

    The forwarded result of block.

  • Runs the given block inside a transaction.

    Takes care of flattening nested transaction calls into a single transaction, and rolling back changes for you on error. You can e.g. wrap multiple put calls into a single write transaction to increase performance by re-using the resources of a single transaction to read and write data.

    Declaration

    Swift

    public func runInTransaction(_ block: () -> Void)

    Parameters

    block

    Code that needs to run in a read/write transaction.

  • Runs the given block inside a read(-only) transaction.

    Takes care of flattening nested transaction calls into a single transaction, and rolling back changes for you on error. You can e.g. wrap multiple get calls into a single read transaction to increase performance by re-using the resources for reading data into memory.

    You can nest read-only transaction into read/write transactions, but not vice versa.

    Throws

    NSError with OBXErrorDomain when an action inside the block throws.

    Declaration

    Swift

    public func runInReadOnlyTransaction(_ block: () throws -> Void) throws

    Parameters

    block

    Code that needs to run in a read or read/write transaction.

  • Runs the given block inside a read(-only) transaction.

    Takes care of flattening nested transaction calls into a single transaction, and rolling back changes for you on error. You can e.g. wrap multiple get calls into a single read transaction to increase performance by re-using the resources for reading data into memory.

    You can nest read-only transaction into read/write transactions, but not vice versa.

    Declaration

    Swift

    public func runInReadOnlyTransaction(_ block: () -> Void)

    Parameters

    block

    Code that needs to run in a read or read/write transaction.