这是我调用获取公共记录的方法:
private func fetchPublicRecordZonesChanges(completion: ErrorHandler?) {
let zone = CKRecordZone.default()
let options = CKFetchRecordZoneChangesOptions()
options.previousServerChangeToken = nil
var records = [CKRecord]()
var recordIDsToDelete = [CKRecordID]()
let operation = CKFetchRecordZoneChangesOperation(recordZoneIDs: [zone.zoneID], optionsByRecordZoneID: [zone.zoneID: options])
operation.database = CloudAssistant.shared.publicDatabase
operation.recordChangedBlock = { record in
records.append(record)
}
operation.recordWithIDWasDeletedBlock = { recordID, string in
recordIDsToDelete.append(recordID)
}
operation.recordZoneChangeTokensUpdatedBlock = { _, token, _ in
if let token = token {
Token.temporaryPublicZoneServerChangeToken = token
}
}
operation.recordZoneFetchCompletionBlock = { [weak self] _, token, _, _, error in
if let error = error, error.isTokenExpiredError {
UserDefaults.remove(forKey: PublicZoneServerChangeTokenKey)
self?.fetchPublicRecordZonesChanges(completion: completion)
return
}
if let token = token {
Token.temporaryPublicZoneServerChangeToken = token
}
}
operation.fetchRecordZoneChangesCompletionBlock = { [weak self] error in
self?.save(records: records, recordIDsToDelete: recordIDsToDelete) { error in
completion?(error)
}
}
operationQueue.addOperation(operation)
}
但是,虽然调用了这个方法,但没有任何东西被提取,甚至一旦调用recordChangedBlock
闭包也没有。为什么?
我很确定我有记录:
并且环境也很好,因为私有开发记录被正确获取(当然使用不同的方法)。我究竟做错了什么?
要收到有关公共数据库更改的通知,请为您关注的记录类型创建CKQuerySubscription
。这是一个例子:
let subscription = CKQuerySubscription(
recordType: "Question",
predicate: NSPredicate(value: true),
subscriptionID: "subscriptionQuestion",
options: [
.firesOnRecordCreation,
.firesOnRecordUpdate,
.firesOnRecordDeletion
])
let info = CKNotificationInfo()
info.shouldSendContentAvailable = true
subscription.notificationInfo = info
let operation = CKModifySubscriptionsOperation(subscriptionsToSave: [subscription], subscriptionIDsToDelete: nil)
operation.modifySubscriptionsCompletionBlock = { saved, deleted, error in
if let error = error{
print("Add subscription error: \(error)")
}else{
print("Successfully added Question subscription.")
}
}
//:::
let container = CKContainer(identifier: "...")
container.publicCloudDatabase.add(operation)