KeyValueStorage
public protocol KeyValueStorage
This protocol describes the interface of a key value storage, where the keys
are String
s and the values are Data
objects.
-
A key is the identifier for a specific value stored in the key-value storage. The key is unique.
Declaration
Swift
typealias Key = String
-
A value can contain arbitrary data and is identified by a key.
Declaration
Swift
typealias Value = Bytes
-
Store a value in the key-value storage for a given key.
Example
storage.store(key: "name", value: Data("John Doe".utf8)) { optionalError in if let error = optionalError { // TODO Handle error } }
Declaration
Parameters
value
The value that should be stored.
key
The key that identifies the value.
finished
A closure that is called asynchronuously once the operation is finished.
error
An optional error that might have occurred during storing.
-
Retrieve a value from the key-value storage for a given key.
Example
storage.retrieve(for: "name") { optionalValue, optionalError in precondition((optionalValue != nil) == (optionalError != nil)) guard let value = optionalValue else { let error = optionalError! // TODO Handle error return } // Success, do something with `value` }
Postcondition
(
value
=nil
) ⊻ (error
=nil
)Declaration
Parameters
key
The key that identifies the value.
finished
A closure that is called asynchronuously once the operation is finished.
value
The value if no error occurred,
nil
else.error
An optional error that might have occurred during storing.
-
Remove the value from the key-value storage for a given key.
Example
storage.remove(key: "name") { optionalError in if let error = optionalError { // TODO Handle error } }
Declaration
Swift
func remove(for key: Key, finished: @escaping (_ error: Error?) -> Void)
Parameters
key
The key that identifies the value.
finished
A closure that is called asynchronuously once the operation is finished.
error
An optional error that might have occurred during storing.