Skip to content

Commit 0b6b24a

Browse files
committed
fix issue #238
1 parent 634f8a2 commit 0b6b24a

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

IceCream/Classes/CKRecordRecoverable.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,23 @@ extension CKRecordRecoverable where Self: Object {
7777
if let existObject = realm.object(ofType: U.self, forPrimaryKey: primaryKeyValue) {
7878
uList.append(existObject)
7979
} else {
80-
pendingUTypeRelationshipsWorker.addToPendingListElement(propertyName: prop.name, primaryKeyValue: primaryKeyValue)
80+
pendingUTypeRelationshipsWorker.addToPendingList(elementPrimaryKeyValue: primaryKeyValue, propertyName: prop.name, owner: o)
8181
}
8282
}
8383

8484
if schema.className == V.className() {
8585
if let existObject = realm.object(ofType: V.self, forPrimaryKey: primaryKeyValue) {
8686
vList.append(existObject)
8787
} else {
88-
pendingVTypeRelationshipsWorker.addToPendingListElement(propertyName: prop.name, primaryKeyValue: primaryKeyValue)
88+
pendingVTypeRelationshipsWorker.addToPendingList(elementPrimaryKeyValue: primaryKeyValue, propertyName: prop.name, owner: o)
8989
}
9090
}
9191

9292
if schema.className == W.className() {
9393
if let existObject = realm.object(ofType: W.self, forPrimaryKey: primaryKeyValue) {
9494
wList.append(existObject)
9595
} else {
96-
pendingWTypeRelationshipsWorker.addToPendingListElement(propertyName: prop.name, primaryKeyValue: primaryKeyValue)
96+
pendingWTypeRelationshipsWorker.addToPendingList(elementPrimaryKeyValue: primaryKeyValue, propertyName: prop.name, owner: o)
9797
}
9898
}
9999

IceCream/Classes/PendingRelationshipsWorker.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@
88
import Foundation
99
import RealmSwift
1010

11+
/// PendingRelationshipsWorker is responsible for temporarily storing relationships when objects recovering from CKRecord
1112
final class PendingRelationshipsWorker<Element: Object> {
1213

1314
var realm: Realm?
14-
var owner: Object?
1515

16-
var pendingListElementPrimaryKeyValue: [AnyHashable: String] = [:]
16+
var pendingListElementPrimaryKeyValue: [AnyHashable: (String, Object)] = [:]
1717

18-
func addToPendingListElement(propertyName: String, primaryKeyValue: AnyHashable) {
19-
pendingListElementPrimaryKeyValue[primaryKeyValue] = propertyName
18+
func addToPendingList(elementPrimaryKeyValue: AnyHashable, propertyName: String, owner: Object) {
19+
pendingListElementPrimaryKeyValue[elementPrimaryKeyValue] = (propertyName, owner)
2020
}
2121

2222
func resolvePendingListElements() {
23-
guard let owner = owner, let realm = realm, pendingListElementPrimaryKeyValue.count > 0 else {
23+
guard let realm = realm, pendingListElementPrimaryKeyValue.count > 0 else {
2424
// Maybe we could add one log here
2525
return
2626
}
2727
BackgroundWorker.shared.start {
28-
for (primaryKeyValue, propName) in self.pendingListElementPrimaryKeyValue {
28+
for (primaryKeyValue, (propName, owner)) in self.pendingListElementPrimaryKeyValue {
2929
guard let list = owner.value(forKey: propName) as? List<Element> else { return }
3030
if let existListElementObject = realm.object(ofType: Element.self, forPrimaryKey: primaryKeyValue) {
3131
try! realm.write {

IceCream/Classes/SyncObject.swift

-6
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,8 @@ extension SyncObject: Syncable {
9494
print("There is something wrong with the converson from cloud record to local object")
9595
return
9696
}
97-
98-
self.pendingUTypeRelationshipsWorker.owner = object
9997
self.pendingUTypeRelationshipsWorker.realm = realm
100-
101-
self.pendingVTypeRelationshipsWorker.owner = object
10298
self.pendingVTypeRelationshipsWorker.realm = realm
103-
104-
self.pendingWTypeRelationshipsWorker.owner = object
10599
self.pendingWTypeRelationshipsWorker.realm = realm
106100

107101
/// 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:).

0 commit comments

Comments
 (0)