我从CloudKit Server获取记录更改,但每次出现错误
[LogFacilityCK]操作E0367CA861DAF5A9出现连接错误: 错误域= NSCocoaErrorDomain代码= 4097“连接到名为的服务 com.apple.cloudd“UserInfo = {NSDebugDescription =与服务的连接 名为com.apple.cloudd}
我知道原因是因为我错误地初始化了CKFetchRecordZoneChangesOperation 请检查以下代码并纠正我:
CKFetchRecordZoneChangesOperation *operation = [[CKFetchRecordZoneChangesOperation alloc]initWithRecordZoneIDs:@[ recordZoneID ] optionsByRecordZoneID:@{@"Zone":recordZoneID,@"FetchRecordZoneChangesOptions":option}];
我需要这方面的帮助:
**@{@"Zone":recordZoneID,@"FetchRecordZoneChangesOptions":option}];**
答案 0 :(得分:5)
只需创建变量:
var serverChangeToken: CKServerChangeToken? {
guard let encoded = UserDefaults(suiteName: SharedGroupName)?.data(forKey: ServerChangeTokenKey) else {
return nil
}
return NSKeyedUnarchiver.unarchiveObject(with: encoded) as? CKServerChangeToken
}
然后初始化一个对象:
let zone = CKRecordZone(zoneName: "my_custom_zone_name")
let options = CKFetchRecordZoneChangesOptions()
options.previousServerChangeToken = serverChangeToken
let operation = CKFetchRecordZoneChangesOperation(recordZoneIDs: [zone.zoneID], optionsByRecordZoneID: [zone.zoneID: options])
答案 1 :(得分:2)
即使使用正确的CKServerChangeToken,我也遇到了这个错误,因为我没有控制数组changedZoneIDs
有0个元素的情况。
严格地说,您需要两个单独的CKServerChangeToken来访问数据库更改和zoneChanges(比方说databaseChangeToken
和zoneChangeToken
)。您可以像BartłomiejSemańczyk所说的那样创建它们
- 使用recordZoneWithIDChangedBlock获取更改区域的zoneID
-use fetchDatabaseChangesCompletionBlock保存databaseChangeToken并调用CKFetchRecordZoneChangesOperation,使用zoneChangeToken,
-each zone有自己的changeToken,所以我最终得到了这个解决方案:
let operation = CKFetchDatabaseChangesOperation(previousServerChangeToken: databaseChangeToken)
var changedZoneIDs: [CKRecordZoneID] = []
operation.recordZoneWithIDChangedBlock = { (zoneID) in
changedZoneIDs.append(zoneID)
}
operation.fetchDatabaseChangesCompletionBlock = { (databaseToken, moreComing, error) in
{
//manage moreComing
//save databaseToken
//manage error
if changedZoneIDs.count > 0
{
var optionsByRecordZoneID = [CKRecordZoneID: CKFetchRecordZoneChangesOptions]()
for zoneID in changedZoneIDs {
let changeTokenData = UserDefaults.standard.value(forKey: "\(zoneID.zoneName) zoneChangeToken") as? Data // Read change token from disk
var zoneChangeToken:CKServerChangeToken?
if (changeTokenData != nil){
zoneChangeToken = NSKeyedUnarchiver.unarchiveObject(with: changeTokenData!)as! CKServerChangeToken?
}
let options = CKFetchRecordZoneChangesOptions()
options.previousServerChangeToken = zoneChangeToken
optionsByRecordZoneID[zoneID] = options
}
let operation = CKFetchRecordZoneChangesOperation(recordZoneIDs: changedZoneIDs, optionsByRecordZoneID: optionsByRecordZoneID)
// ...
}