|
|
|
|
@ -27,14 +27,7 @@ protocol SomeCollection: CollectionHolder, Identifiable { |
|
|
|
|
func allItems() -> [any Storable] |
|
|
|
|
|
|
|
|
|
func loadDataFromServerIfAllowed() async throws |
|
|
|
|
|
|
|
|
|
// func resetApiCalls() |
|
|
|
|
|
|
|
|
|
// func deleteApiCallById(_ id: String) async throws |
|
|
|
|
// func apiCallById(_ id: String) async -> (any SomeCall)? |
|
|
|
|
|
|
|
|
|
// func hasPendingAPICalls() async -> Bool |
|
|
|
|
// func contentOfApiCallFile() async -> String? |
|
|
|
|
func loadCollectionsFromServerIfNoFile() async throws |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -98,20 +91,6 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
self._inMemory = inMemory |
|
|
|
|
self._sendsUpdate = sendsUpdate |
|
|
|
|
self._store = store |
|
|
|
|
// self.loadCompletion = loadCompletion |
|
|
|
|
|
|
|
|
|
// if synchronized { |
|
|
|
|
// let apiCallCollection = ApiCallCollection<T>() |
|
|
|
|
// self.apiCallsCollection = apiCallCollection |
|
|
|
|
// Task { |
|
|
|
|
// do { |
|
|
|
|
// try await apiCallCollection.loadFromFile() |
|
|
|
|
// } catch { |
|
|
|
|
// Logger.error(error) |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
self._load() |
|
|
|
|
} |
|
|
|
|
@ -178,16 +157,8 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
} else { |
|
|
|
|
self._setItems(decoded) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// DispatchQueue.main.async { |
|
|
|
|
// Logger.log("loaded \(T.fileName()) with \(decoded.count) items") |
|
|
|
|
// self.items = decoded |
|
|
|
|
// self._updateIndexIfNecessary() |
|
|
|
|
//// self.loadCompletion?(self) |
|
|
|
|
// NotificationCenter.default.post(name: NSNotification.Name.CollectionDidLoad, object: self) |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
// else { |
|
|
|
|
} |
|
|
|
|
// else if self.synchronized { |
|
|
|
|
// Task { |
|
|
|
|
// do { |
|
|
|
|
// try await self.loadDataFromServerIfAllowed() |
|
|
|
|
@ -196,7 +167,7 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _setItems(_ items: [T]) { |
|
|
|
|
@ -215,7 +186,7 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
/// Retrieves the data from the server and loads it into the items array |
|
|
|
|
public func loadDataFromServerIfAllowed() async throws { |
|
|
|
|
guard self.synchronized, !(self is StoredSingleton<T>) else { |
|
|
|
|
throw StoreError.unSynchronizedCollection |
|
|
|
|
throw StoreError.cannotSyncCollection(name: self.resourceName) |
|
|
|
|
} |
|
|
|
|
do { |
|
|
|
|
let items: [T] = try await self._store.getItems() |
|
|
|
|
@ -229,6 +200,13 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func loadCollectionsFromServerIfNoFile() async throws { |
|
|
|
|
let fileURL: URL = try self._store.fileURL(type: T.self) |
|
|
|
|
if !FileManager.default.fileExists(atPath: fileURL.path()) { |
|
|
|
|
try await self.loadDataFromServerIfAllowed() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// MARK: - Basic operations |
|
|
|
|
|
|
|
|
|
/// Adds or updates the provided instance inside the collection |
|
|
|
|
|