multistore
Laurent 2 years ago
parent de3dc84ba7
commit d38b619cb8
  1. 9
      LeStorage/Store.swift
  2. 65
      LeStorage/StoredCollection.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<T : Storable>(synchronized: Bool, indexed: Bool = false) -> StoredCollection<T> {
public func registerCollection<T : Storable>(synchronized: Bool, indexed: Bool = false, inMemory: Bool = false) -> StoredCollection<T> {
// register collection
let collection = StoredCollection<T>(synchronized: synchronized, store: Store.main, indexed: indexed, loadCompletion: nil)
let collection = StoredCollection<T>(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()

@ -30,6 +30,9 @@ public class StoredCollection<T: Storable>: 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<T: Storable>: 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<T: Storable>: 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<T>) -> ())? = nil) {
init(synchronized: Bool, store: Store, indexed: Bool = false, asynchronousIO: Bool = true, inMemory: Bool = false, loadCompletion: ((StoredCollection<T>) -> ())? = 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<T: Storable>: 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<T: Storable>: 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()
// }
// }
}

Loading…
Cancel
Save