change loadDataFromServerIfAllowed to an async method

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

@ -248,7 +248,9 @@ public class Store {
public func loadCollectionFromServer() {
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 deleteApiCallById(_ id: String) throws
func reset()
func loadDataFromServerIfAllowed() throws
func loadDataFromServerIfAllowed() async throws
var synchronized: Bool { get }
func hasPendingAPICalls() -> Bool
}
@ -116,7 +116,9 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
do {
if self._inMemory {
try self.loadDataFromServerIfAllowed()
Task {
try await self.loadDataFromServerIfAllowed()
}
} else {
try self._loadFromFile()
}
@ -171,18 +173,16 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
}
/// 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 {
throw StoreError.unSynchronizedCollection
}
Task {
do {
let items: [T] = try await self._store.getItems()
try self._addOrUpdate(contentOfs: items, shouldSync: false)
self._hasChanged = true
} catch {
Logger.error(error)
}
do {
let items: [T] = try await self._store.getItems()
try self._addOrUpdate(contentOfs: items, shouldSync: false)
self._hasChanged = true
} catch {
Logger.error(error)
}
}

Loading…
Cancel
Save