CompositeStorage
open class CompositeStorage : CompositeCodableStoring
Composite Storage
Represents an in-memory dictionary storage and physical storage. The CompositeStorage instance is not thread safe. Using directly from multiple threads can lead to unexpected behaviors.
To initialize a default CompositeStorage
you would use the following lines somewhere central in your application. Then it’s ready to use.
var compositeStore = CompositeStorage()
// by default it is initialized with a MemoryDataStorage() and a PlistCoder()
do {
// setting an example store
let secureKeyValueStore = SecureKeyValueStore()
try secureKeyValueStore.open(with: "your_encryption_key")
try self.compositeStore.setPersistentStore(secureKeyValueStorage)
} catch {
//error handling
}
You can insert to the CompositeStorage
instance.
let testCustomObject = CustomTestStruct()
// put data to compositeStore
do {
try self.compositeStore.put(testCustomObject, key: "testCustomObject")
let returnDataFromMemory = try self.compositeStore.get(CustomTestStruct.self, key: "testCustomObject")
} catch {
//error handling
}
Load data from CompositeStorage
do {
let returnData = try self.compositeStore.get(CustomTestStruct.self, key: "testCustomObject")
} catch {
//error handling
}
Remove a key from compositeStore
do {
try self.compositeStore.removeData(for: key)
} catch {
//error handling
}
-
Persistent store to save data from memory store
Declaration
Swift
public var persistentStore: DataStoring? { get }
-
Public Initializer of CompositeStorage
Declaration
Swift
public init(memoryStore: DataStoring = MemoryDataStorage(), coder: CoderProtocol = PlistCoder())
Parameters
memoryStore
a store which conforms to DataStoring protocol. By default it is a MemoryDataStorage object
coder
a Coder object which is able to encode and decode data. Conforms to CoderProtocol. By default it is a PlistCoder()
-
Loads data from composite store
Throws
SecureStorageError.Closed
if theCompositeCodableStoring
compliant object is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Declaration
Swift
open func get<T>(_ type: T.Type, for key: String) throws -> T? where T : Decodable, T : Encodable
Parameters
type
the requested type, which has to conform to Codable protocol
key
a String object which identifies the data as a key
Return Value
the requested data type if the key exists, or nil if not exists
-
Inserts data to composite store
Throws
SecureStorageError.Closed
if theCompositeCodableStoring
compliant object is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailed
if the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Declaration
Swift
open func put<T>(_ value: T, for key: String) throws where T : Decodable, T : Encodable
Parameters
value
the insertable value which is a type conforming Codable protocol
key
a String which identifies the value in the storage
-
Removes data for the given key in storage
Throws
SecureStorageError.Closed
if theCompositeCodableStoring
compliant object is locked with its encryption key.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Declaration
Swift
open func remove(for key: String) throws
Parameters
key
a String which identifies the data in the storage
-
Returns the Set of keys which are existing in the store
Throws
SecureStorageError.Closed
if theCompositeCodableStoring
compliant object is locked with its encryption key.Throws
SecureStorageError.BackingStoreError
if an error occured on the underlying store. Code corresponds to sqlite3 error codes (https://www.sqlite.org/c3ref/c_abort.html).Declaration
Swift
open func keys() throws -> Set<String>
Return Value
Set of Strings as keys
-
Sets the persistentStore of compositeStore Until this is not set, the data will be inserted only in the given memoryStore
Throws
CompositeStorageError.persistentStoreAlreadySet if there is the persistentStore was previously setDeclaration
Swift
open func setPersistentStore(_ store: DataStoring) throws
Parameters
store
a storage which conforms to DataStoring protocol
-
Declaration
Swift
open func releasePersistentStore()