MemoryDataStorage
public class MemoryDataStorage : DataStoring
Memory Data Store
Represents an in-memory dictionary storage. The MemoryDataStorage instance is not thread safe. Using directly from multiple threads can lead to unexpected behaviors.
To initialize a default MemoryDataStorage
you would use the following lines somewhere central in your application. Then it’s ready to use.
var memoryDatabaseStore = MemoryDatabaseStore()
You can insert to the MemoryDataStorage
instance as long as the application is running.
let stringToEncode = "exampleStringToEncode"
if let encodedData = stringToEncode.data(using: .utf8) {
do {
try self.memoryStore.put(data: encodedData, for: "dataKey")
} catch {
// handle error
}
}
// Load data from store
var returnData: Data?
do {
returnData = try self.memoryStore.data(for: "encodedDataKey")
} catch {
// handle error
}
// Remove data from store
var returnData: Data?
do {
returnData = try self.memoryStore.remove(for: "encodedDataKey"))
} catch {
// handle error
}
-
Public initializer
Declaration
Swift
public init()
-
Loads data to the storage
Throws
SecureStorageErrorDeclaration
Swift
public func put(data: Data, for key: String) throws
Parameters
data
Data object to insert in the storage
key
a String as a key for the insertable object
-
Loads data from storage
Throws
SecureStorageErrorDeclaration
Swift
public func data(for key: String) throws -> Data?
Parameters
key
a String as a key to load from storage
Return Value
the Data object if the key exists, or nil if not exists
-
Removes data from storage if exists
Throws
SecureStorageErrorDeclaration
Swift
public func removeData(for key: String) throws
Parameters
key
a String as a key to remove from storage
-
Loads existing keys from storage
Throws
Declaration
Swift
public func keys() throws -> Set<String>
Return Value
a set of Strings which contains all of the available keys