DataStoreTransaction

public struct DataStoreTransaction : ~Copyable, DataItemExtractor

A DataStoreTransaction, a ~Copyable struct passed borrowing to a DataLayer.TransactionBlock given to DataLayer.transactionally(execute:).

It wraps a pending DataStore edit session, offering the write helpers (apply, put, putAll, remove, clear) and commit as synchronous methods.

It conforms to DataItemExtractor, so all the typed getters of that protocol (get(key:as:), getArray(key:of:), …) and of JSONPathExtractable (extract(path:as:), …) are available directly on the transaction, on top of the getDataItem(key:) read requirement.

Note

DataStoreTransaction is noncopyable and is passed borrowing to the TransactionBlock, so it cannot be copied, stored in a property, or captured by an escaping closure. It is only valid for the duration of the block.
  • Applies an edit (DataStoreEdit.put or DataStoreEdit.remove) to the pending transaction.

    The DataLayer will not be changed until the transaction is committed. Applying after commit does nothing.

    Declaration

    Swift

    public func apply(_ edit: DataStoreEdit)

    Parameters

    edit

    The DataStoreEdit to be applied once the transaction is committed.

  • Adds a single key-value pair to the pending transaction.

    The DataLayer will not be changed until the transaction is committed. Putting after commit does nothing.

    Declaration

    Swift

    public func put(key: String, value: DataInput, expiry: Expiry)

    Parameters

    key

    The key to store the value under.

    value

    The DataInput to be stored.

    expiry

    The time frame for this data to remain stored.

  • Adds all key-value pairs from the DataObject to the pending transaction.

    The DataLayer will not be changed until the transaction is committed. Putting after commit does nothing.

    Declaration

    Swift

    public func putAll(dataObject: DataObject, expiry: Expiry)

    Parameters

    dataObject

    A DataObject containing the key-value pairs to be stored.

    expiry

    The time frame for this data to remain stored.

  • Adds the removal of an individual key to the pending transaction.

    The DataLayer will not be changed until the transaction is committed. Removing after commit does nothing.

    Declaration

    Swift

    public func remove(key: String)

    Parameters

    key

    The key to remove from the DataLayer.

  • Adds the removal of multiple keys to the pending transaction.

    The DataLayer will not be changed until the transaction is committed. Removing after commit does nothing.

    Declaration

    Swift

    public func remove(keys: [String])

    Parameters

    keys

    The list of keys to remove from the DataLayer.

  • Adds the removal of all the entries of the DataLayer to the pending transaction, before then adding any other key-value pair.

    The DataLayer will not be changed until the transaction is committed. Clearing after commit does nothing.

    Declaration

    Swift

    public func clear()
  • Reads the current, already committed, value for a key from the DataLayer.

    Applied, but not committed, edits won’t be reflected in the value returned by this method.

    This is the only read requirement of DataItemExtractor: all its other typed getters, such as get(key:as:), are derived from it.

    Declaration

    Swift

    public func getDataItem(key: String) -> DataItem?

    Parameters

    key

    The key used to look up data in the DataLayer.

    Return Value

    The DataItem if one was found for the provided key.

  • Reads a dictionary containing all the current, already committed, data in the DataLayer.

    Applied, but not committed, edits won’t be reflected in the value returned by this method.

    Declaration

    Swift

    public func getAll() -> DataObject

    Return Value

    A DataObject dictionary with all the data currently stored in the DataLayer.

  • Reads all the keys currently, already committed, stored in the DataLayer.

    Applied, but not committed, edits won’t be reflected in the value returned by this method.

    Declaration

    Swift

    public func keys() -> [String]

    Return Value

    A list of all string keys present in the DataLayer.

  • Reads the number of entries currently, already committed, stored in the DataLayer.

    Applied, but not committed, edits won’t be reflected in the value returned by this method.

    Declaration

    Swift

    public func count() -> Int

    Return Value

    The count of all key-value pairs in the DataLayer.

  • Commits the pending edits. It will commit all the edits that were previously applied.

    Committing more than once does nothing, even if the first commit had nothing to write or failed.

    Throws

    An error when the transaction fails.

    Declaration

    Swift

    public func commit() throws