diff --git a/IceCream/Classes/CKRecordRecoverable.swift b/IceCream/Classes/CKRecordRecoverable.swift index 69ade1b1..cf40180f 100644 --- a/IceCream/Classes/CKRecordRecoverable.swift +++ b/IceCream/Classes/CKRecordRecoverable.swift @@ -77,7 +77,7 @@ extension CKRecordRecoverable where Self: Object { if let existObject = realm.object(ofType: U.self, forPrimaryKey: primaryKeyValue) { uList.append(existObject) } else { - pendingUTypeRelationshipsWorker.addToPendingListElement(propertyName: prop.name, primaryKeyValue: primaryKeyValue) + pendingUTypeRelationshipsWorker.addToPendingList(elementPrimaryKeyValue: primaryKeyValue, propertyName: prop.name, owner: o) } } @@ -85,7 +85,7 @@ extension CKRecordRecoverable where Self: Object { if let existObject = realm.object(ofType: V.self, forPrimaryKey: primaryKeyValue) { vList.append(existObject) } else { - pendingVTypeRelationshipsWorker.addToPendingListElement(propertyName: prop.name, primaryKeyValue: primaryKeyValue) + pendingVTypeRelationshipsWorker.addToPendingList(elementPrimaryKeyValue: primaryKeyValue, propertyName: prop.name, owner: o) } } @@ -93,7 +93,7 @@ extension CKRecordRecoverable where Self: Object { if let existObject = realm.object(ofType: W.self, forPrimaryKey: primaryKeyValue) { wList.append(existObject) } else { - pendingWTypeRelationshipsWorker.addToPendingListElement(propertyName: prop.name, primaryKeyValue: primaryKeyValue) + pendingWTypeRelationshipsWorker.addToPendingList(elementPrimaryKeyValue: primaryKeyValue, propertyName: prop.name, owner: o) } } diff --git a/IceCream/Classes/PendingRelationshipsWorker.swift b/IceCream/Classes/PendingRelationshipsWorker.swift index 4ed8398d..0b4103ba 100644 --- a/IceCream/Classes/PendingRelationshipsWorker.swift +++ b/IceCream/Classes/PendingRelationshipsWorker.swift @@ -8,24 +8,24 @@ import Foundation import RealmSwift +/// PendingRelationshipsWorker is responsible for temporarily storing relationships when objects recovering from CKRecord final class PendingRelationshipsWorker { var realm: Realm? - var owner: Object? - var pendingListElementPrimaryKeyValue: [AnyHashable: String] = [:] + var pendingListElementPrimaryKeyValue: [AnyHashable: (String, Object)] = [:] - func addToPendingListElement(propertyName: String, primaryKeyValue: AnyHashable) { - pendingListElementPrimaryKeyValue[primaryKeyValue] = propertyName + func addToPendingList(elementPrimaryKeyValue: AnyHashable, propertyName: String, owner: Object) { + pendingListElementPrimaryKeyValue[elementPrimaryKeyValue] = (propertyName, owner) } func resolvePendingListElements() { - guard let owner = owner, let realm = realm, pendingListElementPrimaryKeyValue.count > 0 else { + guard let realm = realm, pendingListElementPrimaryKeyValue.count > 0 else { // Maybe we could add one log here return } BackgroundWorker.shared.start { - for (primaryKeyValue, propName) in self.pendingListElementPrimaryKeyValue { + for (primaryKeyValue, (propName, owner)) in self.pendingListElementPrimaryKeyValue { guard let list = owner.value(forKey: propName) as? List else { return } if let existListElementObject = realm.object(ofType: Element.self, forPrimaryKey: primaryKeyValue) { try! realm.write { diff --git a/IceCream/Classes/SyncObject.swift b/IceCream/Classes/SyncObject.swift index ac3cd804..8a74b6de 100644 --- a/IceCream/Classes/SyncObject.swift +++ b/IceCream/Classes/SyncObject.swift @@ -94,14 +94,8 @@ extension SyncObject: Syncable { print("There is something wrong with the converson from cloud record to local object") return } - - self.pendingUTypeRelationshipsWorker.owner = object self.pendingUTypeRelationshipsWorker.realm = realm - - self.pendingVTypeRelationshipsWorker.owner = object self.pendingVTypeRelationshipsWorker.realm = realm - - self.pendingWTypeRelationshipsWorker.owner = object self.pendingWTypeRelationshipsWorker.realm = realm /// If your model class includes a primary key, you can have Realm intelligently update or add objects based off of their primary key values using Realm().add(_:update:).