From 102be89a83d348f341a6918872baeb1143f8c5f7 Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 17 May 2024 12:23:39 +0200 Subject: [PATCH] Adds a boolean indicating if the collection has been loaded from the server --- LeStorage/StoredCollection.swift | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/LeStorage/StoredCollection.swift b/LeStorage/StoredCollection.swift index afbaa30..737f4c0 100644 --- a/LeStorage/StoredCollection.swift +++ b/LeStorage/StoredCollection.swift @@ -52,6 +52,7 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti /// Provides fast access for instances if the collection has been instanced with [indexed] = true fileprivate var _indexes: [String : T]? = nil + /// Collection of API calls used to store HTTP calls fileprivate var apiCallsCollection: StoredCollection>? = nil /// Indicates whether the collection has changed, thus requiring a write operation @@ -68,8 +69,11 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti } } - /// Denotes a collection that loads and writes asynchronousIO + /// Denotes a collection that loads and writes asynchronously fileprivate var asynchronousIO: Bool = true + + /// Indicates if the collection has loaded objects from the server + fileprivate(set) public var hasLoadedFromServer: Bool = false init(synchronized: Bool, store: Store, indexed: Bool = false, asynchronousIO: Bool = true, inMemory: Bool = false, sendsUpdate: Bool = true, loadCompletion: ((StoredCollection) -> ())? = nil) { self.synchronized = synchronized @@ -129,7 +133,7 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti } fileprivate func _loadFromFile() throws { - let url: URL = try self._urlForJSONFile() +// let url: URL = try self._urlForJSONFile() if self.asynchronousIO { Task(priority: .high) { @@ -157,7 +161,7 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti } } else { DispatchQueue.main.async { - Logger.log("collection \(T.fileName()) has no file yet") +// Logger.log("collection \(T.fileName()) has no file yet") self.loadCompletion?(self) NotificationCenter.default.post(name: NSNotification.Name.CollectionDidLoad, object: self) } @@ -181,6 +185,8 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti let items: [T] = try await self._store.getItems() try self._addOrUpdate(contentOfs: items, shouldSync: false) self._hasChanged = true + self.hasLoadedFromServer = true + NotificationCenter.default.post(name: NSNotification.Name.CollectionDidLoad, object: self) } catch { Logger.error(error) } @@ -297,6 +303,10 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti } } + public func deleteAll() throws { + try self.delete(contentOfs: self.items) + } + // MARK: - SomeCall /// Returns the collection items as [any Storable]