Skip to content

Commit a15c7e7

Browse files
Sol Caicaiyue1993
Sol Cai
authored andcommitted
add support for the conversion of recordID to Int type primaryKey
1 parent c822b5d commit a15c7e7

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

IceCream/Classes/CKRecordRecoverable.swift

+10-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ extension CKRecordRecoverable where Self: Object {
7676
if let asset = record.value(forKey: prop.name) as? CKAsset {
7777
recordValue = CreamAsset.parse(from: prop.name, record: record, asset: asset)
7878
} else if let owner = record.value(forKey: prop.name) as? CKRecord.Reference, let ownerType = prop.objectClassName {
79-
recordValue = realm.dynamicObject(ofType: ownerType, forPrimaryKey: owner.recordID.recordName)
79+
recordValue = realm.dynamicObject(ofType: ownerType, forPrimaryKey: primaryKeyForRecordID(recordID: owner.recordID))
8080
// Because we use the primaryKey as recordName when object converting to CKRecord
8181
}
8282
default:
@@ -88,4 +88,13 @@ extension CKRecordRecoverable where Self: Object {
8888
}
8989
return o
9090
}
91+
92+
/// The primaryKey in Realm could be type of Int or String. However the `recordName` is a String type, we need to make a check.
93+
/// The reversed process happens in `recordID` property in `CKRecordConvertible` protocol.
94+
///
95+
/// - Parameter recordID: the recordID that CloudKit sent to us
96+
/// - Returns: the specific value of primaryKey in Realm
97+
static func primaryKeyForRecordID(recordID: CKRecord.ID) -> Any {
98+
return Int(recordID.recordName) ?? recordID.recordName
99+
}
91100
}

IceCream/Classes/SyncObject.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ extension SyncObject: Syncable {
9191

9292
public func delete(recordID: CKRecord.ID) {
9393
DispatchQueue.main.async {
94-
guard let object = self.realm.object(ofType: T.self, forPrimaryKey: recordID.recordName) else {
94+
guard let object = self.realm.object(ofType: T.self, forPrimaryKey: T.primaryKeyForRecordID(recordID: recordID)) else {
9595
// Not found in local realm database
9696
return
9797
}

0 commit comments

Comments
 (0)