ios - 无法使用 cloudkit 从公共(public)数据库中获取记录

标签 ios swift cloudkit

这是我用来获取公共(public)记录的方法:

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 闭包时也没有获取任何内容。为什么?

我很确定我在那里有记录:

enter image description here

环境也很好,因为私有(private)开发记录被正确获取(当然使用不同的方法)。我做错了什么?

最佳答案

要收到有关您的公共(public)数据库更改的通知,请为您关心的记录类型创建一个 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)

关于ios - 无法使用 cloudkit 从公共(public)数据库中获取记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51834324/

相关文章:

ios - iOS8 CloudKit 可以支持幕后流媒体吗?

ios - 删除 CloudKit 记录 Swift 4

ios - 使用打开的 CV 使用 iPhone 应用程序查找白球

ios - CVOpenGLESTextureCacheCreateTextureFromImage 报告 "Failed to create IOSurface image (texture)"

ios - 当我使用 NSArray 时 NSPredicate 崩溃

ios - 如何在 Swift 2.0 中将 UITextField 转换为整数?

swift - 在 UISearchController iOS 11 上使用背景图片

ios - 变量显示为 nil - swift 4 IOS

ios - 为什么我们要覆盖最初为 nil 的变量而不是简单地设置它们?

ios - 有什么方法可以从CloudKit迁移数据吗?