operation.recordChangedBlock {}
がfunc fetchZoneChanges(database: CKDatabase, databaseTokenKey: String, zoneIDs: [CKRecordZoneID], completion: @escaping() -> Void)
内に終了したとき、Core DataデータベースのcoreDataレコードをCloudKit 。再帰的に-saveを呼び出すための再帰呼び出しを試みる-save:CloudKitレコードが変更された後に、Core Data viewContextを保存するときに中止されたコンテキストで
エラーの試み:私はコンテキストを保存しようとしているときの状況には表示され中止されました。ここで
func updateCoreDataRecord(editedRecord: Record, newName: String, handleComplete_RecordEditedInCoreData:(()->())) {
let record_RecordID = editedRecord.recordID!
let request: NSFetchRequest<Record> = Record.fetchRequest()
request.predicate = NSPredicate(format: "recordID = %@", record_RecordID)
do {
let results = try self.viewContext?.fetch(request)
if results?.count == 1 {
let recordToUpdate = results![0]
recordToUpdate.setValue(newName, forKey: "name")
DispatchQueue.main.async { // Here I receive the error when editing goes from CloudKit
self.saveViewContext()
}
handleComplete_RecordEditedInCoreData()
}
} catch {
print(error)
}
}
その他の関連する関数です:
func saveViewContext() {
let context = self.getViewContext()
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
func getViewContext() -> NSManagedObjectContext {
return self.persistentContainer.viewContext
}
私のアプリのすべてのviewContext
呼び出しがメインスレッドで作られています。私はそれがエラーを引き起こす可能性があると思います。
エラーはsaveViewContext
メソッドにつながります。私は確信していませんが、これは並行性の問題かもしれないと思うが、私はそれを修正する方法を知らない。
CloudKitレコードを更新するときにこのエラーを回避するには、コードを書き直すべきですか?私はメインのキューで実行するすべての呼び出しをラップしようとしましたが、これは役に立たなかった。
私も同じupdateCoreDataRecord()
方法を使用しています、ユーザーはデバイス上のコアデータレコードを変更し、これは何のエラーを引き起こさない。私は、レコードのコアデータの更新を行うoperation.recordChangedBlock {}内のCloudKitからRecord Changesを受け取ったときにのみこのエラーが発生します。