|
|
|
|
@ -114,6 +114,9 @@ public class SyncedCollection<T : SyncedStorable>: BaseCollection<T>, SomeSynced |
|
|
|
|
if let index = self.items.firstIndex(where: { $0.id == instance.id }) { |
|
|
|
|
if self.updateItem(instance, index: index, shouldBeSynchronized: true) { |
|
|
|
|
self.setChanged() |
|
|
|
|
if instance.shared == true { |
|
|
|
|
self._cleanUpSharedDependencies() |
|
|
|
|
} |
|
|
|
|
return (instance, false) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
@ -146,6 +149,9 @@ public class SyncedCollection<T : SyncedStorable>: BaseCollection<T>, SomeSynced |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
self._cleanUpSharedDependencies() |
|
|
|
|
|
|
|
|
|
return batch |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
@ -232,6 +238,39 @@ public class SyncedCollection<T : SyncedStorable>: BaseCollection<T>, SomeSynced |
|
|
|
|
await self._sendOperationBatch(batch) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _cleanUpSharedDependencies() { |
|
|
|
|
for relationship in T.relationships() { |
|
|
|
|
if let syncedType = relationship.type as? (any SyncedStorable.Type) { |
|
|
|
|
do { |
|
|
|
|
try self._deleteUnusedSharedInstances(relationship: relationship, type: syncedType) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _deleteUnusedSharedInstances<S: SyncedStorable>(relationship: Relationship, type: S.Type) throws { |
|
|
|
|
|
|
|
|
|
let store: Store |
|
|
|
|
if relationship.mainStoreLookup { |
|
|
|
|
store = self.store.storeCenter.mainStore |
|
|
|
|
} else { |
|
|
|
|
store = self.store |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let collection: SyncedCollection<S> = try store.syncedCollection() |
|
|
|
|
collection._deleteUnusedSharedInstances() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _deleteUnusedSharedInstances() { |
|
|
|
|
|
|
|
|
|
let sharedItems = self.items.filter { $0.shared == true } |
|
|
|
|
for sharedItem in sharedItems { |
|
|
|
|
self.store.deleteUnusedSharedIfNecessary(sharedItem) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// MARK: - Asynchronous operations |
|
|
|
|
|
|
|
|
|
/// Adds or update an instance asynchronously and waits for network operations |
|
|
|
|
@ -297,6 +336,16 @@ public class SyncedCollection<T : SyncedStorable>: BaseCollection<T>, SomeSynced |
|
|
|
|
self.deleteItem(instance, shouldBeSynchronized: false) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func deleteUnusedShared(instance: T) { |
|
|
|
|
|
|
|
|
|
guard instance.shared == true else { return } |
|
|
|
|
|
|
|
|
|
// Delete the instance and its non-used shared dependencies |
|
|
|
|
self.deleteUnusedShared(instance, shouldBeSynchronized: false) |
|
|
|
|
|
|
|
|
|
self.setChanged() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Deletes the instance in the collection without synchronization |
|
|
|
|
func deleteByStringIdNoSync(_ id: String) { |
|
|
|
|
defer { |
|
|
|
|
|