此应用使用CloudKit,我在本地将数据同步到Core Data。我相信我的基础工作有一个例外。以下代码检查CloudKit中的更改并允许我保存到Core Data但我无法将下载的更改限制为单个recordType(我有两个recordTypes," Patient"" PatientList& #34)。我认为CKFetchRecordZoneChangesOptions()应该允许按recordType进行过滤,但我读过和尝试的所有内容只允许在所有recordTypes中通过desiredKeys进行过滤,这当然对我的目的没有用。我可以通过recordType限制核心数据保存,但这似乎是错误的方法。
任何指导都将不胜感激。 Xcode 8.3.3 iOS10 Swift 3
func checkUpdates() {
let operation = CKFetchDatabaseChangesOperation(previousServerChangeToken: changeToken)
let myZone : CKRecordZone = CKRecordZone(zoneName: "myPatientZone")
var zonesIDs : [CKRecordZoneID] = []
//you ONLY want changes from myPatientZone
//you ONLY allow one custom zone and you ONLY share myPatientZone
operation.recordZoneWithIDChangedBlock = { (zoneID) in
if zoneID == myZone.zoneID {
zonesIDs.append(zoneID)
}//if
}//recordZoneWithIDChangedBlock
operation.changeTokenUpdatedBlock = { (token) in
self.changeToken = token
}
operation.fetchDatabaseChangesCompletionBlock = { (token, more, error) in
if error == nil && !zonesIDs.isEmpty {
self.changeToken = token
let options = CKFetchRecordZoneChangesOptions()
options.previousServerChangeToken = self.fetchChangeToken
let fetchOperation = CKFetchRecordZoneChangesOperation(recordZoneIDs: zonesIDs, optionsByRecordZoneID: [zonesIDs[0] : options])
fetchOperation.recordChangedBlock = { (record) in
let recordName = record.recordID.recordName
let request : NSFetchRequest<Patient> = Patient.fetchRequest()
request.predicate = NSPredicate(format: "recordName = %@", recordName)
do {
let result = try self.persistentContainer.viewContext.fetch(request)
//if the record is not in the local core data
if result.isEmpty {
let patient = Patient(context: self.persistentContainer.viewContext)
patient.recordName = recordName
patient.firstName = record.object(forKey: "firstname") as? String
//all the other fields here...
try self.persistentContainer.viewContext.save()
} else {
//if the record is in core data but has been updated
let patient = result[0]
//patient.recordName - don't change
patient.firstName = record.object(forKey: "firstname") as?
//all the other fields
}//if result.isEmpty
} catch {
//add the CRS custom error handler
print("Error")
}//do catch
}//fetchOperation
//fetchOperation.recordWithIDWasDeletedBlock = { (recordID, recordType) in
//fetchOperation.recordZoneChangeTokensUpdatedBlock = { (zoneID, token, data) in
database.add(operation)
}//checkUpdates