|
|
|
|
@ -23,7 +23,7 @@ protocol CollectionHolder { |
|
|
|
|
protocol SomeCollection: CollectionHolder, Identifiable { |
|
|
|
|
var resourceName: String { get } |
|
|
|
|
var synchronized: Bool { get } |
|
|
|
|
var hasLoadedLocally: Bool { get } |
|
|
|
|
var hasLoaded: Bool { get } |
|
|
|
|
|
|
|
|
|
func allItems() -> [any Storable] |
|
|
|
|
|
|
|
|
|
@ -75,10 +75,7 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
fileprivate var asynchronousIO: Bool = true |
|
|
|
|
|
|
|
|
|
/// Indicates if the collection has loaded locally, with or without a file |
|
|
|
|
fileprivate(set) public var hasLoadedLocally: Bool = false |
|
|
|
|
|
|
|
|
|
/// Indicates if the collection has loaded objects from the server |
|
|
|
|
fileprivate(set) public var hasLoadedFromServer: Bool = false |
|
|
|
|
fileprivate(set) public var hasLoaded: Bool = false |
|
|
|
|
|
|
|
|
|
init(synchronized: Bool, store: Store, indexed: Bool = false, asynchronousIO: Bool = true, inMemory: Bool = false, sendsUpdate: Bool = true) { |
|
|
|
|
self.synchronized = synchronized |
|
|
|
|
@ -141,8 +138,6 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
/// Decodes the json file into the items array |
|
|
|
|
fileprivate func _decodeJSONFile() throws { |
|
|
|
|
|
|
|
|
|
defer { self.hasLoadedLocally = true } |
|
|
|
|
|
|
|
|
|
let fileURL = try self._store.fileURL(type: T.self) |
|
|
|
|
|
|
|
|
|
if FileManager.default.fileExists(atPath: fileURL.path()) { |
|
|
|
|
@ -176,8 +171,10 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _setAsLoaded() { |
|
|
|
|
self.hasLoadedLocally = true |
|
|
|
|
NotificationCenter.default.post(name: NSNotification.Name.CollectionDidLoad, object: self) |
|
|
|
|
self.hasLoaded = true |
|
|
|
|
DispatchQueue.main.async { |
|
|
|
|
NotificationCenter.default.post(name: NSNotification.Name.CollectionDidLoad, object: self) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _setItems(_ items: [T]) { |
|
|
|
|
@ -202,13 +199,10 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
if items.count > 0 { |
|
|
|
|
try self._addOrUpdate(contentOfs: items, shouldSync: false) |
|
|
|
|
} |
|
|
|
|
self.hasLoadedFromServer = true |
|
|
|
|
DispatchQueue.main.async { |
|
|
|
|
NotificationCenter.default.post(name: NSNotification.Name.CollectionDidLoad, object: self) |
|
|
|
|
} |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
self._setAsLoaded() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func loadCollectionsFromServerIfNoFile() async throws { |
|
|
|
|
@ -364,6 +358,18 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
try self.delete(contentOfs: self.items) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// MARK: - Migrations |
|
|
|
|
|
|
|
|
|
public func insertAllIntoCurrentService() { |
|
|
|
|
for item in self.items { |
|
|
|
|
self._sendInsertionIfNecessary(item) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public func insertIntoCurrentService(item: T) { |
|
|
|
|
self._sendInsertionIfNecessary(item) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// MARK: - SomeCall |
|
|
|
|
|
|
|
|
|
/// Returns the collection items as [any Storable] |
|
|
|
|
|