Store

public class Store : CustomDebugStringConvertible

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

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)
let personBox = store.box(for: Person.self)
let persons = personBox.all()
  • Returns the version number of ObjectBox C API the framework was built against.

    Declaration

    Swift

    public static var version: String { get }
  • The path that was passed to this instance when creating it.

    Declaration

    Swift

    internal(set) public var directoryPath: String
  • Create a new store.

    Declaration

    Swift

    public init(model: OpaquePointer, directory: String, maxDbSizeInKByte: UInt64, fileMode: UInt32,
                maxReaders: UInt32) throws

    Parameters

    model

    A model description generated using ObjectBox.modelBuilder

    directory

    The path to thedirectory in which ObjectBox is to save the files related to the database.

    maxDbSizeInKByte

    Maximum size the database may take up on disk.

    fileMode

    The unix permissions (like 0o755) to use for creating the database files.

    maxReaders

    How many threads may be reading at the same time at once.

  • Return a box for reading/writing entities of the given class from/to the database. Obtain a a Box for the given type.

    Declaration

    Swift

    public func box<T>(for entityType: T.Type = T.self) -> Box<T> where T : EntityInspectable, T : __EntityRelatable, T == T.EntityBindingType.EntityType

    Parameters

    entityType

    Object type to get a box for.

    Return Value

    Box for the given type.

  • Delete the database files on disk. This Store object will not be usable after calling this.

    Declaration

    Swift

    public func closeAndDeleteAllFiles() throws
  • Obtain a Box for the given relation’s target type.

    Declaration

    Swift

    public func box<T>(for relation: ToOne<T>) -> Box<T> where T : EntityInspectable, T : __EntityRelatable, T == T.EntityBindingType.EntityType

    Parameters

    relation

    ToOne relation for an object type.

    Return Value

    Box for the relation’s target type.

  • Runs the given block inside a read/write 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

    rethrows errors thrown inside, plus any ObjectBoxError that makes sense. There is also a no-throw version that logs ObjectBoxErrors instead.

    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 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

    rethrows errors thrown inside, plus any ObjectBoxError that makes sense. There is also a no-throw version that logs ObjectBoxErrors instead.

    Declaration

    Swift

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

    Parameters

    block

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

    Return Value

    The forwarded result of block.