|
|
|
|
@ -33,6 +33,9 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
/// Doesn't write the collection in a file |
|
|
|
|
fileprivate var _inMemory: Bool = false |
|
|
|
|
|
|
|
|
|
/// Indicates if the synchronized collection sends update to the API |
|
|
|
|
fileprivate var _sendsUpdate: Bool = true |
|
|
|
|
|
|
|
|
|
/// The list of stored items |
|
|
|
|
@Published public fileprivate(set) var items: [T] = [] |
|
|
|
|
|
|
|
|
|
@ -66,13 +69,14 @@ 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, inMemory: Bool = false, loadCompletion: ((StoredCollection<T>) -> ())? = nil) { |
|
|
|
|
init(synchronized: Bool, store: Store, indexed: Bool = false, asynchronousIO: Bool = true, inMemory: Bool = false, sendsUpdate: Bool = true, loadCompletion: ((StoredCollection<T>) -> ())? = nil) { |
|
|
|
|
self.synchronized = synchronized |
|
|
|
|
self.asynchronousIO = asynchronousIO |
|
|
|
|
if indexed { |
|
|
|
|
self._index = [:] |
|
|
|
|
} |
|
|
|
|
self._inMemory = inMemory |
|
|
|
|
self._sendsUpdate = sendsUpdate |
|
|
|
|
self._store = store |
|
|
|
|
self.loadCompletion = loadCompletion |
|
|
|
|
|
|
|
|
|
@ -103,7 +107,7 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _loadFromFile() throws { |
|
|
|
|
let url = try FileUtils.pathForFileInDocumentDirectory(T.fileName()) |
|
|
|
|
let url: URL = try FileUtils.pathForFileInDocumentDirectory(T.fileName()) |
|
|
|
|
if FileManager.default.fileExists(atPath: url.path()) { |
|
|
|
|
|
|
|
|
|
if self.asynchronousIO { |
|
|
|
|
@ -120,7 +124,7 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
|
|
|
|
|
/// Decodes the json file into the items array |
|
|
|
|
fileprivate func _decodeJSONFile() throws { |
|
|
|
|
let jsonString = try FileUtils.readDocumentFile(fileName: T.fileName()) |
|
|
|
|
let jsonString: String = try FileUtils.readDocumentFile(fileName: T.fileName()) |
|
|
|
|
if let decoded: [T] = try jsonString.decodeArray() { |
|
|
|
|
DispatchQueue.main.async { |
|
|
|
|
Logger.log("loaded \(T.fileName()) with \(decoded.count) items") |
|
|
|
|
@ -353,7 +357,7 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
|
|
|
|
|
/// Sends an update api call for the provided [instance] |
|
|
|
|
fileprivate func _sendUpdateIfNecessary(_ instance: T) throws { |
|
|
|
|
guard self.synchronized else { |
|
|
|
|
guard self.synchronized, self._sendsUpdate else { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -408,7 +412,7 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _rescheduleApiCalls() { |
|
|
|
|
return |
|
|
|
|
// return |
|
|
|
|
|
|
|
|
|
guard let apiCallsCollection, apiCallsCollection.isNotEmpty else { |
|
|
|
|
return |
|
|
|
|
|