You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
LeStorage/LeStorage/StoredSingleton.swift

36 lines
952 B

//
// StoredObject.swift
// LeStorage
//
// Created by Laurent Morvillier on 03/05/2024.
//
import Foundation
/// A class extending the capabilities of StoredCollection but supposedly manages only one item
public class StoredSingleton<T: Storable>: StoredCollection<T> {
/// Sets the singleton to the collection without synchronizing it
public func setItemNoSync(_ instance: T) {
self.setSingletonNoSync(instance: instance)
}
/// updates the existing singleton
public func update() throws {
if let item = self.item() {
try self.addOrUpdate(instance: item)
}
}
/// Returns the singleton
public func item() -> T? {
return self.items.first
}
// MARK: - Protects from use
public override func addOrUpdate(contentOfs sequence: any Sequence<T>) throws {
fatalError("method unavailable for StoredSingleton, use update")
}
}