From d38b619cb8766dd7cb82e2ce47dd77746582da5d Mon Sep 17 00:00:00 2001 From: Laurent Date: Tue, 16 Apr 2024 10:39:28 +0200 Subject: [PATCH] fix update --- LeStorage/Store.swift | 9 +++-- LeStorage/StoredCollection.swift | 65 +++++++++++++++----------------- 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/LeStorage/Store.swift b/LeStorage/Store.swift index 193cc9d..71aa40b 100644 --- a/LeStorage/Store.swift +++ b/LeStorage/Store.swift @@ -72,10 +72,10 @@ public class Store { /// Registers a collection /// [synchronize] denotes a collection which modification will be sent to the django server - public func registerCollection(synchronized: Bool, indexed: Bool = false) -> StoredCollection { + public func registerCollection(synchronized: Bool, indexed: Bool = false, inMemory: Bool = false) -> StoredCollection { // register collection - let collection = StoredCollection(synchronized: synchronized, store: Store.main, indexed: indexed, loadCompletion: nil) + let collection = StoredCollection(synchronized: synchronized, store: Store.main, indexed: indexed, inMemory: inMemory, loadCompletion: nil) self._collections[T.resourceName()] = collection // if synchronized { // register additional collection for api calls @@ -96,8 +96,9 @@ public class Store { } } - public func currentUserUUID() throws -> UUID { - if let uuidString = self.settingsStorage.item.userUUIDString, let uuid = UUID(uuidString: uuidString) { + public func currentUserUUID() -> UUID { + if let uuidString = self.settingsStorage.item.userUUIDString, + let uuid = UUID(uuidString: uuidString) { return uuid } else { let uuid = UIDevice.current.identifierForVendor ?? UUID() diff --git a/LeStorage/StoredCollection.swift b/LeStorage/StoredCollection.swift index aaedf63..8dfa3df 100644 --- a/LeStorage/StoredCollection.swift +++ b/LeStorage/StoredCollection.swift @@ -30,6 +30,9 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti /// If true, will synchronize the data with the provided server located at the Store's synchronizationApiURL let synchronized: Bool + /// Doesn't write the collection in a file + fileprivate var _inMemory: Bool = false + /// The list of stored items @Published public fileprivate(set) var items: [T] = [] @@ -48,7 +51,10 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti fileprivate var _hasChanged: Bool = false { didSet { if self._hasChanged == true { - self._scheduleWrite() + + if !self._inMemory { + self._scheduleWrite() + } DispatchQueue.main.async { NotificationCenter.default.post(name: NSNotification.Name.CollectionDidChange, object: self) } @@ -60,12 +66,13 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti /// Denotes a collection that loads and writes asynchronousIO fileprivate var asynchronousIO: Bool = true - init(synchronized: Bool, store: Store, indexed: Bool = false, asynchronousIO: Bool = true, loadCompletion: ((StoredCollection) -> ())? = nil) { + init(synchronized: Bool, store: Store, indexed: Bool = false, asynchronousIO: Bool = true, inMemory: Bool = false, loadCompletion: ((StoredCollection) -> ())? = nil) { self.synchronized = synchronized self.asynchronousIO = asynchronousIO if indexed { self._index = [:] } + self._inMemory = inMemory self._store = store self.loadCompletion = loadCompletion @@ -82,29 +89,35 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti /// Migrates if necessary and asynchronously decodes the json file fileprivate func _load() { + do { - let url = try FileUtils.pathForFileInDocumentDirectory(T.fileName()) - if FileManager.default.fileExists(atPath: url.path()) { - - if self.asynchronousIO { - Task(priority: .high) { - try await Store.main.performMigrationIfNecessary(self) - try self._decodeJSONFile() - } - } else { - try self._decodeJSONFile() - } - + if self._inMemory { + try self.loadDataFromServer() + } else { + try self._loadFromFile() } - // else { - // try? self.loadDataFromServer() - // } } catch { - Logger.log(error) + Logger.error(error) } } + fileprivate func _loadFromFile() throws { + let url = try FileUtils.pathForFileInDocumentDirectory(T.fileName()) + if FileManager.default.fileExists(atPath: url.path()) { + + if self.asynchronousIO { + Task(priority: .high) { + try await Store.main.performMigrationIfNecessary(self) + try self._decodeJSONFile() + } + } else { + try self._decodeJSONFile() + } + + } + } + /// Decodes the json file into the items array fileprivate func _decodeJSONFile() throws { let jsonString = try FileUtils.readDocumentFile(fileName: T.fileName()) @@ -355,22 +368,6 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti } } } - - - -// Task { -// do { -// if let apiCall = try self._callForInstance(instance, method: Method.put) { -// try self._prepareCall(apiCall: apiCall) -// _ = try await self._store.execute(apiCall: apiCall) -// } -// -//// let _ = try await self._store.service?.update(instance) -// } catch { -// Logger.error(error) -// self.rescheduleApiCallsIfNecessary() -// } -// } }