gpt4 book ai didi

swift - iOS 8 : being notified of CKRecord changes via subscriptions

转载 作者:搜寻专家 更新时间:2023-10-31 22:23:50 24 4
gpt4 key购买 nike

我正在运行 CKFetchNotificationChangesOperation 来提取自上次我的应用程序运行以来应该发送的所有更新通知。

正如我所料,我得到了一堆 CKNotification 对象,但是这些 CKNotification 中的每一个的 recordFields 对象都是 nil。我希望它会填充我的对象的所有已更改属性。但也许事实并非如此。

我没有指定 CKSubscription 对象的 desiredKeys 属性,这显然会强制 CKNotification recordFields 保存该数据。但是文档说 desiredKeys 只能有 3 个值,而我的对象有超过 3 个可以更新的值。

所以现在我在想,我只需要获取 CKNotification 对象,看看它是否是一个更新,然后重新获取它指向的对象以检索它的数据。然后我可以对新的 CKRecord 与旧的进行比较,看看有什么变化。

我理解正确吗?

最佳答案

首先你需要存储CKFetchNotificationChangesOperation提供的CKServerChangeToken

var notificationChangesOperation = CKFetchNotificationChangesOperation(previousServerChangeToken: previousChangeToken)

var fetchedRecordIDs:[CKRecordID]()

//we just care about inserts, we don't care about of changes of records existing in database, that's why it's enough just to save recordIDs
notificationChangesOperation.notificationChangedBlock = {notification in

let queryNotif = notification as CKQueryNotification
// println("fetched CKQueryNotification \(queryNotif)")

if (!contains(fetchedRecordIDs, queryNotif.recordID)) {
fetchedRecordIDs.append(queryNotif.recordID)
}
}

notificationChangesOperation.fetchNotificationChangesCompletionBlock = {serverChangeToken, error in
if (error) {
println("failed to fetch notification \(error)")
}

self.previousChangeToken = serverChangeToken

completionHandler(recordIDs: fetchedRecordIDs, error: error)
}

container.addOperation(notificationChangesOperation)

var previousChangeToken:CKServerChangeToken? {
get {
let encodedObjectData = NSUserDefaults.standardUserDefaults().objectForKey(SubscriptionKeys.PreviousChangeToken) as? NSData

if (encodedObjectData) {
return NSKeyedUnarchiver.unarchiveObjectWithData(encodedObjectData) as? CKServerChangeToken
}

return nil
}
set(newToken) {
if (newToken) {
println("new token \(newToken)")

NSUserDefaults.standardUserDefaults().setObject(NSKeyedArchiver.archivedDataWithRootObject(newToken), forKey:SubscriptionKeys.PreviousChangeToken)
}
}
}

然后在 CKFetchNotificationChangesOperation 的下一次调用中传递它。然后此操作将为您提供自上次请求以来的更新。 CKNotification (CKQueryNotification) 为您提供CKRecordID 对象,而不是CKRecord

因此,您需要使用 CKFetchRecordsOperation 为这些 ID 重新获取所有 CKRecord,并相应地更新您的模型对象。

func queryMessagesWithIDs(IDs:[CKRecordID], completionHandler: ([CKRecord]!, NSError!) -> Void) {
var fetchRecordsOperation = CKFetchRecordsOperation(recordIDs: IDs)

var messages:[CKRecord]()

fetchRecordsOperation.perRecordCompletionBlock = {record, recordID, error in
if (!error) {
messages.append(record)
} else {
println("failed to get message with ID \(recordID.recordName)")
}
}

fetchRecordsOperation.fetchRecordsCompletionBlock = {_, error in
if (error) {
println(error)
} else {
messages.sort{$0.creationDate.compare($1.creationDate) == NSComparisonResult.OrderedAscending}
}

dispatch_async(dispatch_get_main_queue(), {completionHandler(messages, error)})
}

publicDatabase.addOperation(fetchRecordsOperation)
}

关于swift - iOS 8 : being notified of CKRecord changes via subscriptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24747799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com