fix collection loading

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

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

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

Loading…
Cancel
Save