StorageDelegate

open class StorageDelegate<T> : Storage, @unchecked Sendable where T : Decodable, T : Encodable, T : Sendable

A storage delegate class that delegates its operations to a storage. It can optionally cache the stored item in memory. This class is designed to be subclassed by specific storage strategies (e.g., keychain, in-memory) that conform to the Storage protocol.

Parameters

T

The type of the object being stored. Must conform to Codable to ensure that object can be easily encoded and decoded.

  • Initializer for StorageDelegate

    Declaration

    Swift

    public init(delegate: any Storage<T>, cacheable: Bool = false)

    Parameters

    delegate

    The storage to delegate the operations to.

    cacheable

    Whether the storage delegate should cache the object in memory.

  • save(item:) Asynchronous

    Saves the given item in the storage and optionally in memory.

    Declaration

    Swift

    public func save(item: T) async throws

    Parameters

    item

    The item to save.

  • get() Asynchronous

    Retrieves the item from memory if it’s cached, otherwise from the storage.

    Declaration

    Swift

    public func get() async throws -> T?

    Return Value

    The item if it exists, nil otherwise.

  • delete() Asynchronous

    Deletes the item from the storage and removes it from memory if it’s cached.

    Declaration

    Swift

    public func delete() async throws