multistore
Laurent 2 years ago
parent de3dc84ba7
commit d38b619cb8
  1. 9
      LeStorage/Store.swift
  2. 45
      LeStorage/StoredCollection.swift

@ -72,10 +72,10 @@ public class Store {
/// Registers a collection /// Registers a collection
/// [synchronize] denotes a collection which modification will be sent to the django server /// [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 // 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 self._collections[T.resourceName()] = collection
// if synchronized { // register additional collection for api calls // if synchronized { // register additional collection for api calls
@ -96,8 +96,9 @@ public class Store {
} }
} }
public func currentUserUUID() throws -> UUID { public func currentUserUUID() -> UUID {
if let uuidString = self.settingsStorage.item.userUUIDString, let uuid = UUID(uuidString: uuidString) { if let uuidString = self.settingsStorage.item.userUUIDString,
let uuid = UUID(uuidString: uuidString) {
return uuid return uuid
} else { } else {
let uuid = UIDevice.current.identifierForVendor ?? UUID() 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 /// If true, will synchronize the data with the provided server located at the Store's synchronizationApiURL
let synchronized: Bool let synchronized: Bool
/// Doesn't write the collection in a file
fileprivate var _inMemory: Bool = false
/// The list of stored items /// The list of stored items
@Published public fileprivate(set) var items: [T] = [] @Published public fileprivate(set) var items: [T] = []
@ -48,7 +51,10 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
fileprivate var _hasChanged: Bool = false { fileprivate var _hasChanged: Bool = false {
didSet { didSet {
if self._hasChanged == true { if self._hasChanged == true {
if !self._inMemory {
self._scheduleWrite() self._scheduleWrite()
}
DispatchQueue.main.async { DispatchQueue.main.async {
NotificationCenter.default.post(name: NSNotification.Name.CollectionDidChange, object: self) 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 /// Denotes a collection that loads and writes asynchronousIO
fileprivate var asynchronousIO: Bool = true 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.synchronized = synchronized
self.asynchronousIO = asynchronousIO self.asynchronousIO = asynchronousIO
if indexed { if indexed {
self._index = [:] self._index = [:]
} }
self._inMemory = inMemory
self._store = store self._store = store
self.loadCompletion = loadCompletion self.loadCompletion = loadCompletion
@ -82,7 +89,20 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
/// Migrates if necessary and asynchronously decodes the json file /// Migrates if necessary and asynchronously decodes the json file
fileprivate func _load() { fileprivate func _load() {
do { do {
if self._inMemory {
try self.loadDataFromServer()
} else {
try self._loadFromFile()
}
} catch {
Logger.error(error)
}
}
fileprivate func _loadFromFile() throws {
let url = try FileUtils.pathForFileInDocumentDirectory(T.fileName()) let url = try FileUtils.pathForFileInDocumentDirectory(T.fileName())
if FileManager.default.fileExists(atPath: url.path()) { if FileManager.default.fileExists(atPath: url.path()) {
@ -96,13 +116,6 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
} }
} }
// else {
// try? self.loadDataFromServer()
// }
} catch {
Logger.log(error)
}
} }
/// Decodes the json file into the items array /// Decodes the json file into the items array
@ -356,22 +369,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()
// }
// }
} }
/// Sends an delete api call for the provided [instance] /// Sends an delete api call for the provided [instance]

Loading…
Cancel
Save