|
|
|
|
@ -227,7 +227,7 @@ public class StoreCenter { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _store(id: String?) -> Store? { |
|
|
|
|
fileprivate func _store(id: String?) -> Store { |
|
|
|
|
if let storeId = id, let store = self._stores[storeId] { |
|
|
|
|
return store |
|
|
|
|
} else { |
|
|
|
|
@ -782,7 +782,7 @@ public class StoreCenter { |
|
|
|
|
@MainActor |
|
|
|
|
func synchronizationDelete<T: SyncedStorable>(id: String, type: T.Type, storeId: String?) { |
|
|
|
|
do { |
|
|
|
|
try self._store(id: storeId)?.deleteNoSyncNoCascade(type: type, id: id) |
|
|
|
|
try self._store(id: storeId).deleteNoSyncNoCascade(type: type, id: id) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
@ -794,23 +794,34 @@ public class StoreCenter { |
|
|
|
|
func synchronizationRevoke<T: SyncedStorable>(id: String, type: T.Type, storeId: String?) { |
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
if self._instanceShared(id: id, type: type) { |
|
|
|
|
let count = self.mainStore.referenceCount(type: type, id: id) |
|
|
|
|
if count == 0 { |
|
|
|
|
try self._store(id: storeId)?.deleteNoSyncNoCascade(type: type, id: id) |
|
|
|
|
if let instance = self._instance(id: id, type: type, storeId: storeId) { |
|
|
|
|
if instance.sharing != nil && !self.isReferenced(instance: instance) { |
|
|
|
|
try self._store(id: storeId).deleteNoSyncNoCascade(type: type, id: id) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// if self._instanceShared(id: id, type: type, storeId: storeId) { |
|
|
|
|
// let count = self.isReferenced(type: type, id: id) |
|
|
|
|
// if count == 0 { |
|
|
|
|
// try self._store(id: storeId).deleteNoSyncNoCascade(type: type, id: id) |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Returns whether an instance has been shared with the user |
|
|
|
|
fileprivate func _instanceShared<T: SyncedStorable>(id: String, type: T.Type) -> Bool { |
|
|
|
|
fileprivate func _instance<T: SyncedStorable>(id: String, type: T.Type, storeId: String?) -> T? { |
|
|
|
|
let realId: T.ID = T.buildRealId(id: id) |
|
|
|
|
let instance: T? = self.mainStore.findById(realId) |
|
|
|
|
return instance?.sharing != nil |
|
|
|
|
return self._store(id: storeId).findById(realId) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Returns whether an instance has been shared with the user |
|
|
|
|
// fileprivate func _instanceShared<T: SyncedStorable>(id: String, type: T.Type, storeId: String?) -> Bool { |
|
|
|
|
//// let realId: T.ID = T.buildRealId(id: id) |
|
|
|
|
// let instance: T? = self._instance(id: id, type: type, storeId: storeId) |
|
|
|
|
// return instance?.sharing != nil |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
/// Deletes a data log by data id |
|
|
|
|
fileprivate func _cleanupDataLog(dataId: String) { |
|
|
|
|
@ -826,6 +837,28 @@ public class StoreCenter { |
|
|
|
|
self._deleteLogs.addOrUpdate(instance: dataLog) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func relationshipStore<T: SyncedStorable>(instance: T, relationship: Relationship) -> Store? { |
|
|
|
|
switch relationship.storeLookup { |
|
|
|
|
case .main: return Store.main |
|
|
|
|
case .child: return self._stores[instance.stringId] |
|
|
|
|
case .same: return instance.store |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func isReferenced<T: SyncedStorable>(instance: T) -> Bool { |
|
|
|
|
let relationships = T.relationships() |
|
|
|
|
for relationship in relationships { |
|
|
|
|
if let store = self.relationshipStore(instance: instance, relationship: relationship) { |
|
|
|
|
if store.isReferenced(collectionType: relationship.type, type: T.self, id: instance.stringId) { |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
Logger.w("missing store for instance \(instance)") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// MARK: - Sync data conversion |
|
|
|
|
|
|
|
|
|
func decodeObjectIdentifierDictionary(_ dictionary: [String: Any]) throws -> [ObjectIdentifierArray] { |
|
|
|
|
|