FilesStore
open class FilesStore<T> where T : Identifiable, T : Decodable, T : Encodable
FilesStore offers a convenient way to store a collection of Codable objects in the files system.
-
Store’s unique identifier.
Warning: Never use the same identifier for two -or more- different stores.
Declaration
Swift
public let uniqueIdentifier: String -
Store
Expirationoption. default is .neverDeclaration
Swift
public let expiration: Expiration -
JSON encoder. default is JSONEncoder()
Declaration
Swift
open var encoder: JSONEncoder -
JSON decoder. default is JSONDecoder()
Declaration
Swift
open var decoder: JSONDecoder -
Initialize store with given identifiera and an optional expiry duration.
Warning: Never use the same identifier for two -or more- different stores.
Declaration
Swift
required public init(uniqueIdentifier: String, useHashing: Bool = false, expiration: Expiration = .never)Parameters
uniqueIdentifierstore’s unique identifier.
useHashingWhether keys should be hashed before storing or not. default is false
expiryDurationoptional store’s expiry duration default is .never.
-
Save object to store.
Throws
FileManager or JSON encoding error.Declaration
Swift
public func save(_ object: T) throwsParameters
objectobject to save.
-
Save optional object (if not nil) to store.
Throws
FileManager or JSON encoding error.Declaration
Swift
public func save(_ optionalObject: T?) throwsParameters
optionalObjectoptional object to save.
-
Save array of objects to store.
Throws
FileManager or JSON encoding error.Declaration
Swift
public func save(_ objects: [T]) throwsParameters
objectsobject to save.
-
Get object from store by its id.
Declaration
Swift
public func object(withId id: T.ID) -> T?Parameters
idobject id.
Return Value
optional object.
-
Get array of objects from store for array of id values.
Declaration
Swift
public func objects(withIds ids: [T.ID]) -> [T]Parameters
idsarray of ids.
Return Value
array of objects with the given ids.
-
Get all objects from store.
Declaration
Swift
public func allObjects() -> [T]Return Value
array of all objects in store.
-
Delete object by its id from store.
Declaration
Swift
public func delete(withId id: T.ID) throwsParameters
idobject id.
-
Delete objects with ids from given ids array.
Declaration
Swift
public func delete(withIds ids: [T.ID]) throwsParameters
idsarray of ids.
-
Delete all objects in store.
Declaration
Swift
public func deleteAll() -
Count of all objects in store.
Declaration
Swift
public var objectsCount: Int { get } -
Check if store has object with given id.
Declaration
Swift
public func hasObject(withId id: T.ID) -> BoolParameters
idobject id to check for.
Return Value
true if the store has an object with the given id.
-
Iterate over all objects in store.
Declaration
Swift
public func forEach(_ object: (T) -> Void)Parameters
objectiteration block.
FilesStore Class Reference