|
|
|
|
@ -158,18 +158,10 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
} else { |
|
|
|
|
self._setAsLoaded() |
|
|
|
|
} |
|
|
|
|
// else if self.synchronized { |
|
|
|
|
// Task { |
|
|
|
|
// do { |
|
|
|
|
// try await self.loadDataFromServerIfAllowed() |
|
|
|
|
// } catch { |
|
|
|
|
// Logger.error(error) |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Sets the collection as loaded |
|
|
|
|
/// Send a CollectionDidLoad event |
|
|
|
|
fileprivate func _setAsLoaded() { |
|
|
|
|
self.hasLoaded = true |
|
|
|
|
DispatchQueue.main.async { |
|
|
|
|
@ -177,6 +169,7 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Sets a collection of items and indexes them |
|
|
|
|
fileprivate func _setItems(_ items: [T]) { |
|
|
|
|
self.items = items |
|
|
|
|
self._updateIndexIfNecessary() |
|
|
|
|
@ -205,6 +198,7 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
self._setAsLoaded() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Loads the collection using the server data only if the collection file doesn't exists |
|
|
|
|
func loadCollectionsFromServerIfNoFile() async throws { |
|
|
|
|
let fileURL: URL = try self._store.fileURL(type: T.self) |
|
|
|
|
if !FileManager.default.fileExists(atPath: fileURL.path()) { |
|
|
|
|
@ -288,6 +282,7 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
try self._addOrUpdate(contentOfs: sequence) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Adds or update a sequence of elements without synchronizing it |
|
|
|
|
func addOrUpdateNoSync(contentOfs sequence: any Sequence<T>) throws { |
|
|
|
|
try self._addOrUpdate(contentOfs: sequence, shouldSync: false) |
|
|
|
|
} |
|
|
|
|
@ -360,12 +355,14 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
|
|
|
|
|
// MARK: - Migrations |
|
|
|
|
|
|
|
|
|
/// Makes POST ApiCall for all items in the collection |
|
|
|
|
public func insertAllIntoCurrentService() { |
|
|
|
|
for item in self.items { |
|
|
|
|
self._sendInsertionIfNecessary(item) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Makes POST ApiCall for the provided item |
|
|
|
|
public func insertIntoCurrentService(item: T) { |
|
|
|
|
self._sendInsertionIfNecessary(item) |
|
|
|
|
} |
|
|
|
|
@ -410,20 +407,10 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
self.items.removeAll() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Removes the items of the collection, deletes the corresponding file, and also reset the related API calls collection |
|
|
|
|
/// Removes the items of the collection and deletes the corresponding file |
|
|
|
|
public func reset() { |
|
|
|
|
self.items.removeAll() |
|
|
|
|
self._store.removeFile(type: T.self) |
|
|
|
|
// do { |
|
|
|
|
// let url: URL = try T.urlForJSONFile() |
|
|
|
|
// if FileManager.default.fileExists(atPath: url.path()) { |
|
|
|
|
// try FileManager.default.removeItem(at: url) |
|
|
|
|
// } |
|
|
|
|
// } catch { |
|
|
|
|
// Logger.error(error) |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
// self.resetApiCalls() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// MARK: - Reschedule calls |
|
|
|
|
|