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
memoryStorea store which conforms to DataStoring protocol. By default it is a MemoryDataStorage object
codera 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.Closedif theCompositeCodableStoringcompliant object is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailedif the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreErrorif 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 : EncodableParameters
typethe requested type, which has to conform to Codable protocol
keya 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.Closedif theCompositeCodableStoringcompliant object is locked with its encryption key.Throws
SecureStorageError.TypeConversionFailedif the value could not be decoded to the desired return type.Throws
SecureStorageError.BackingStoreErrorif 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 : EncodableParameters
valuethe insertable value which is a type conforming Codable protocol
keya String which identifies the value in the storage
-
Removes data for the given key in storage
Throws
SecureStorageError.Closedif theCompositeCodableStoringcompliant object is locked with its encryption key.Throws
SecureStorageError.BackingStoreErrorif 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) throwsParameters
keya String which identifies the data in the storage
-
Returns the Set of keys which are existing in the store
Throws
SecureStorageError.Closedif theCompositeCodableStoringcompliant object is locked with its encryption key.Throws
SecureStorageError.BackingStoreErrorif 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) throwsParameters
storea storage which conforms to DataStoring protocol
-
Declaration
Swift
open func releasePersistentStore()