iOS SwiftUI云存储之CloudKit如何查询和订阅数据更新

2022-04-12 00:00:00 数据 用户 专区 订阅 自己的

CKQuery 主动查询
CKQuery结合了RecordType,NSPredicate和NSSortDescriptor(可选),为用户提供了要处理的重点数据块。 CloudKit仅支持NSPredictate功能的子集。查询是来自数据库的轮询;它们不应用于一遍又一遍地查询返回相同结果集的数据。

let predicate = NSPredicate(format: "name = %@", "Updated Park Name")
let query = CKQuery(recordType: "Park", predicate: predicate)
let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase
publicDatabase.performQuery(query, inZoneWithID: nil) { (results: [CKRecord]?, error: NSError?) -> Void in
// Handle errors
if error == nil {
for record in results! {
print("\(record)")
}
} }

Subscriptions 等待通知

CKSubcription结合了RecordType,NSPredicate和Push。这允许服务器将任何新更改推送到正在侦听的任何设备,而无需要求设备轮询更改

let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase let predicate = NSPredicate(format: "name = %@", "Updated Park Name") // Send a push notification whenever the record(s)
// found from the predicate change
let subscription = CKSubscription(recordType: "Park", predicate: predicate, options: CKSubscriptionOptions.FiresOnRecordUpdate)
// Tell the application how to handle the notification
let notificationInfo = CKNotificationInfo() notificationInfo.alertLocalizationKey = "LOCAL_NOTIFICATION_KEY" notificationInfo.soundName = "Park.aiff"
notificationInfo.shouldBadge = true subscription.notificationInfo = notificationInfo
// Save the subscription to the server publicDatabase.saveSubscription(subscription) { (subscription: CKSubscription?, error: NSError?) -> Void in
// Handle errors when error != nil }
// Update application:didReceiveRemoteNotification
// to handle CloudKit notifcations
func application(application: NSApplication, didReceiveRemoteNotification userInfo:
[String : AnyObject]) {
let cloudKitNotification = CKNotification(fromRemoteNotificationDictionary: userInfo as! [String: NSObject])
let alertBody = cloudKitNotification.alertBody
if cloudKitNotification.notificationType == CKNotificationType.Query { let queryNotification: CKQueryNotification = cloudKitNotification as! CKQueryNotification
let recordID = queryNotification.recordID
} }

CloudKit User Accounts
CloudKit用户帐户提供了一种使用有关用户的元数据来识别用户的方法。不论客户端是OS X,iOS还是使用CloudKitJS的Web,任何与同一容器通信的客户端都将为登录用户返回相同的ID(清单4-8)。您还可以利用此身份与您自己的服务器进行交互,而无需用户登录.

// This gets the current logged-in user's identifier CKContainer.defaultContainer(). fetchUserRecordIDWithCompletionHandler { (userRecordID: CKRecordID?, error: NSError?) -> Void in
// Handle errors when error != nil }

获取用户相关的元数据 

let defaultContainer = CKContainer.defaultContainer()
let publicDatabase = defaultContainer.publicCloudDatabase defaultContainer.fetchUserRecordIDWithCompletionHandler { (userRecordID: CKRecordID?, error: NSError?) -> Void in
// Handle errors when error != nil if error != nil {} else {
// Get the user record from CloudKit using the
// recordID for the logged-in user publicDatabase.fetchRecordWithID(userRecordID!, completionHandler: { (userRecord: CKRecord?, error: NSError?) -> Void in
// Handle errors
if error != nil {} else {
// User records are like any other records; you // can add key-values and resave them to the
// CloudKit database.
// Assuming we have a displayname
var displayName = userRecord!["displayName"]
print("\(displayName)")
}
}) }
}

出于隐私原因,默认情况下不提供任何个人识别信息。您可以通过CloudKit请求该信息,并且用户必须接受或拒绝提供个人信息。
用户发现允许您从用户那里收集信息(如果他们选择了可发现)。您还可以利用地址簿查找也正在使用服务器的任何联系人,但是他们必须首先启用发现。通过CloudKit利用通讯录不要求用户授权您访问其通讯录;你只是在访问自己的.

let defaultContainer = CKContainer.defaultContainer() defaultContainer.discoverAllContactUserInfosWithCompletionHandler { (userInfos: [CKDiscoveredUserInfo]?, error: NSError?) -> Void in
// Handle errors
if error != nil {} else {
for userInfo in userInfos! {
// familyName will only show if the user
// has oped in to showing it.
print("\(userInfo.userRecordID) \(userInfo.displayContact?.familyName)")
} }
}


————————————————

原文链接:https://blog.csdn.net/iCloudEnd/article/details/106399313


相关文章