|
|
|
@ -435,7 +435,7 @@ public class StoreCenter { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if let updates = json["grants"] as? [String: Any] { |
|
|
|
if let updates = json["grants"] as? [String: Any] { |
|
|
|
try self._parseSyncUpdates(updates) |
|
|
|
try self._parseSyncUpdates(updates, shared: true) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if let revocations = json["revocations"] as? [String: Any] { |
|
|
|
if let revocations = json["revocations"] as? [String: Any] { |
|
|
|
@ -456,7 +456,7 @@ public class StoreCenter { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fileprivate func _parseSyncUpdates(_ updates: [String: Any]) throws { |
|
|
|
fileprivate func _parseSyncUpdates(_ updates: [String: Any], shared: Bool = false) throws { |
|
|
|
for (className, updateData) in updates { |
|
|
|
for (className, updateData) in updates { |
|
|
|
guard let updateArray = updateData as? [[String: Any]] else { |
|
|
|
guard let updateArray = updateData as? [[String: Any]] else { |
|
|
|
Logger.w("Invalid update data for \(className)") |
|
|
|
Logger.w("Invalid update data for \(className)") |
|
|
|
@ -473,7 +473,7 @@ public class StoreCenter { |
|
|
|
let decodedObject = try JSON.decoder.decode(type, from: jsonData) |
|
|
|
let decodedObject = try JSON.decoder.decode(type, from: jsonData) |
|
|
|
|
|
|
|
|
|
|
|
let storeId: String? = decodedObject.getStoreId() |
|
|
|
let storeId: String? = decodedObject.getStoreId() |
|
|
|
StoreCenter.main.synchronizationAddOrUpdate(decodedObject, storeId: storeId) |
|
|
|
StoreCenter.main.synchronizationAddOrUpdate(decodedObject, storeId: storeId, shared: shared) |
|
|
|
} catch { |
|
|
|
} catch { |
|
|
|
Logger.w("Issue with json decoding: \(updateItem)") |
|
|
|
Logger.w("Issue with json decoding: \(updateItem)") |
|
|
|
Logger.error(error) |
|
|
|
Logger.error(error) |
|
|
|
@ -569,11 +569,11 @@ public class StoreCenter { |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func synchronizationAddOrUpdate<T: SyncedStorable>(_ instance: T, storeId: String?) { |
|
|
|
func synchronizationAddOrUpdate<T: SyncedStorable>(_ instance: T, storeId: String?, shared: Bool) { |
|
|
|
let hasAlreadyBeenDeleted: Bool = self._hasAlreadyBeenDeleted(instance) |
|
|
|
let hasAlreadyBeenDeleted: Bool = self._hasAlreadyBeenDeleted(instance) |
|
|
|
if !hasAlreadyBeenDeleted { |
|
|
|
if !hasAlreadyBeenDeleted { |
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { |
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { |
|
|
|
self._store(id: storeId).addOrUpdateIfNewer(instance) |
|
|
|
self._store(id: storeId).addOrUpdateIfNewer(instance, shared: shared) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -596,9 +596,11 @@ public class StoreCenter { |
|
|
|
DispatchQueue.main.async { |
|
|
|
DispatchQueue.main.async { |
|
|
|
do { |
|
|
|
do { |
|
|
|
let type = try StoreCenter.classFromName(model) |
|
|
|
let type = try StoreCenter.classFromName(model) |
|
|
|
let count = Store.main.referenceCount(type: type, id: id) |
|
|
|
if self._instanceShared(id: id, type: type) { |
|
|
|
if count == 0 { |
|
|
|
let count = Store.main.referenceCount(type: type, id: id) |
|
|
|
try self._store(id: storeId).deleteNoSync(type: type, id: id) |
|
|
|
if count == 0 { |
|
|
|
|
|
|
|
try self._store(id: storeId).deleteNoSync(type: type, id: id) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch { |
|
|
|
} catch { |
|
|
|
Logger.error(error) |
|
|
|
Logger.error(error) |
|
|
|
@ -606,6 +608,12 @@ public class StoreCenter { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fileprivate func _instanceShared<T: SyncedStorable>(id: String, type: T.Type) -> Bool { |
|
|
|
|
|
|
|
let realId: T.ID = T.buildRealId(id: id) |
|
|
|
|
|
|
|
let instance: T? = Store.main.findById(realId) |
|
|
|
|
|
|
|
return instance?.shared == true |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fileprivate func _cleanupDataLog(dataId: String) { |
|
|
|
fileprivate func _cleanupDataLog(dataId: String) { |
|
|
|
let logs = self._dataLogs.filter { $0.dataId == dataId } |
|
|
|
let logs = self._dataLogs.filter { $0.dataId == dataId } |
|
|
|
self._dataLogs.delete(contentOfs: logs) |
|
|
|
self._dataLogs.delete(contentOfs: logs) |
|
|
|
|