change loadDataFromServerIfAllowed to an async method

multistore
Laurent 2 years ago
parent 5c21240ca6
commit 7745797974
  1. 4
      LeStorage/Store.swift
  2. 10
      LeStorage/StoredCollection.swift

@ -248,7 +248,9 @@ public class Store {
public func loadCollectionFromServer() { public func loadCollectionFromServer() {
for collection in self._collections.values { for collection in self._collections.values {
try? collection.loadDataFromServerIfAllowed() Task {
try? await collection.loadDataFromServerIfAllowed()
}
} }
} }

@ -18,7 +18,7 @@ protocol SomeCollection: Identifiable {
func deleteById(_ id: String) throws func deleteById(_ id: String) throws
func deleteApiCallById(_ id: String) throws func deleteApiCallById(_ id: String) throws
func reset() func reset()
func loadDataFromServerIfAllowed() throws func loadDataFromServerIfAllowed() async throws
var synchronized: Bool { get } var synchronized: Bool { get }
func hasPendingAPICalls() -> Bool func hasPendingAPICalls() -> Bool
} }
@ -116,7 +116,9 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
do { do {
if self._inMemory { if self._inMemory {
try self.loadDataFromServerIfAllowed() Task {
try await self.loadDataFromServerIfAllowed()
}
} else { } else {
try self._loadFromFile() try self._loadFromFile()
} }
@ -171,11 +173,10 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
} }
/// Retrieves the data from the server and loads it into the items array /// Retrieves the data from the server and loads it into the items array
public func loadDataFromServerIfAllowed() throws { public func loadDataFromServerIfAllowed() async throws {
guard self.synchronized, !(self is StoredSingleton<T>) else { guard self.synchronized, !(self is StoredSingleton<T>) else {
throw StoreError.unSynchronizedCollection throw StoreError.unSynchronizedCollection
} }
Task {
do { do {
let items: [T] = try await self._store.getItems() let items: [T] = try await self._store.getItems()
try self._addOrUpdate(contentOfs: items, shouldSync: false) try self._addOrUpdate(contentOfs: items, shouldSync: false)
@ -184,7 +185,6 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
Logger.error(error) Logger.error(error)
} }
} }
}
// MARK: - Basic operations // MARK: - Basic operations

Loading…
Cancel
Save