Adds a boolean indicating if the collection has been loaded from the server

multistore
Laurent 2 years ago
parent 7745797974
commit 102be89a83
  1. 16
      LeStorage/StoredCollection.swift

@ -52,6 +52,7 @@ public class StoredCollection<T: Storable>: 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<ApiCall<T>>? = nil
/// Indicates whether the collection has changed, thus requiring a write operation
@ -68,8 +69,11 @@ public class StoredCollection<T: Storable>: 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<T>) -> ())? = nil) {
self.synchronized = synchronized
@ -129,7 +133,7 @@ public class StoredCollection<T: Storable>: 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<T: Storable>: 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<T: Storable>: 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<T: Storable>: RandomAccessCollection, SomeCollecti
}
}
public func deleteAll() throws {
try self.delete(contentOfs: self.items)
}
// MARK: - SomeCall
/// Returns the collection items as [any Storable]

Loading…
Cancel
Save