fix collection loading

sync_v2
Laurent 6 months ago
parent fd413ff86a
commit 600fca4fdc
  1. 20
      LeStorage/BaseCollection.swift
  2. 2
      LeStorage/SyncedCollection.swift

@ -73,9 +73,8 @@ public class BaseCollection<T: Storable>: SomeCollection, CollectionHolder {
self.limit = limit self.limit = limit
if synchronousLoading { if synchronousLoading {
self.loadFromFile()
Task { Task {
await self.setAsLoaded() await self.loadFromFile()
} }
} else { } else {
Task(priority: .high) { Task(priority: .high) {
@ -109,23 +108,25 @@ public class BaseCollection<T: Storable>: SomeCollection, CollectionHolder {
/// Migrates if necessary and asynchronously decodes the json file /// Migrates if necessary and asynchronously decodes the json file
func load() async { func load() async {
if !self.inMemory { if !self.inMemory {
self.loadFromFile() await self.loadFromFile()
} else { } else {
await self.setAsLoaded() await MainActor.run {
self.setAsLoaded()
}
} }
} }
/// Starts the JSON file decoding synchronously or asynchronously /// Starts the JSON file decoding synchronously or asynchronously
func loadFromFile() { func loadFromFile() async {
do { do {
try self._decodeJSONFile() try await self._decodeJSONFile()
} catch { } catch {
Logger.error(error) Logger.error(error)
} }
} }
/// Decodes the json file into the items array /// Decodes the json file into the items array
fileprivate func _decodeJSONFile() throws { fileprivate func _decodeJSONFile() async throws {
let fileURL = try self.store.fileURL(type: T.self) let fileURL = try self.store.fileURL(type: T.self)
@ -134,7 +135,10 @@ public class BaseCollection<T: Storable>: SomeCollection, CollectionHolder {
let decoded: [T] = try jsonString.decodeArray() ?? [] let decoded: [T] = try jsonString.decodeArray() ?? []
self.setItems(decoded) self.setItems(decoded)
} }
await MainActor.run {
self.setAsLoaded()
}
} }
/// Sets the collection as loaded /// Sets the collection as loaded

@ -25,7 +25,7 @@ public class SyncedCollection<T : SyncedStorable>: BaseCollection<T>, SomeSynced
if self.inMemory { if self.inMemory {
try await self.loadDataFromServerIfAllowed() try await self.loadDataFromServerIfAllowed()
} else { } else {
self.loadFromFile() await self.loadFromFile()
} }
} catch { } catch {
Logger.error(error) Logger.error(error)

Loading…
Cancel
Save