Skip to content

Commit

Permalink
fix issue #238
Browse files Browse the repository at this point in the history
  • Loading branch information
caiyue1993 committed Jun 27, 2021
1 parent 634f8a2 commit da97e30
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
6 changes: 3 additions & 3 deletions IceCream/Classes/CKRecordRecoverable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,23 @@ 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)
}
}

if schema.className == V.className() {
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)
}
}

if schema.className == W.className() {
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)
}
}

Expand Down
12 changes: 6 additions & 6 deletions IceCream/Classes/PendingRelationshipsWorker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@
import Foundation
import RealmSwift

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

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<Element> else { return }
if let existListElementObject = realm.object(ofType: Element.self, forPrimaryKey: primaryKeyValue) {
try! realm.write {
Expand Down
6 changes: 0 additions & 6 deletions IceCream/Classes/SyncObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:).
Expand Down

0 comments on commit da97e30

Please sign in to comment.