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.putorDataStoreEdit.remove) to the pending transaction.The
DataLayerwill not be changed until the transaction is committed. Applying after commit does nothing.Declaration
Swift
public func apply(_ edit: DataStoreEdit)Parameters
editThe
DataStoreEditto be applied once the transaction is committed. -
Adds all key-value pairs from the
DataObjectto the pending transaction.The
DataLayerwill not be changed until the transaction is committed. Putting after commit does nothing.Declaration
Swift
public func putAll(dataObject: DataObject, expiry: Expiry)Parameters
dataObjectA
DataObjectcontaining the key-value pairs to be stored.expiryThe time frame for this data to remain stored.
-
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 asget(key:as:), are derived from it.Declaration
Swift
public func getDataItem(key: String) -> DataItem?Parameters
keyThe key used to look up data in the
DataLayer.Return Value
The
DataItemif 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() -> DataObjectReturn Value
A
DataObjectdictionary with all the data currently stored in theDataLayer. -
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
View on GitHub