Skip to content

Commit

Permalink
Fix issue with owners and bump version (#3)
Browse files Browse the repository at this point in the history
* fix issue caiyue1993#238

* bump version

* add version control to RealmSwift dependency

Co-authored-by: Yue Cai <[email protected]>
Co-authored-by: Sol Cai <[email protected]>
  • Loading branch information
3 people authored Aug 30, 2021
1 parent e8497a4 commit f95eb1f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
4 changes: 2 additions & 2 deletions IceCream.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'IceCream'
s.version = '2.0.2'
s.version = '2.0.3'
s.summary = 'Sync Realm with CloudKit'
s.description = <<-DESC
Sync Realm Database with CloudKit, written in Swift. It works just like magic.
Expand All @@ -29,5 +29,5 @@ Pod::Spec.new do |s|
s.static_framework = true
s.swift_version = '5.0'

s.dependency 'RealmSwift'
s.dependency 'RealmSwift', '< 10.8.0'
end
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 f95eb1f

Please sign in to comment.