WWDC 2014 Advanced Cloudkit Video建议:每次收到推送时,都应检查通知集合以查找可能错过的任何内容。

我也这样做,但是如果在很短的时间内在同一条记录上发生2次更新,我将收到2个推送通知,每个通知将使用网络两次,因此notificationChangedBlock将被称为2x2 = 4次,但相关的只有2(或0(如果未丢失任何通知)。

这效率不高,您会做些什么?

func application(application: UIApplication!, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]!) {
    Utility.checkNotifications()
}

class func checkNotifications() {

    let defaultContainer = CKContainer.defaultContainer()
    let publicDatabase = defaultContainer.publicCloudDatabase

    let fnco = CKFetchNotificationChangesOperation(previousServerChangeToken: previousChangeToken)
    fnco.notificationChangedBlock = {notification in

        readNotificationIDs.append(notification.notificationID)
        if previousChangeToken != nil {
            Utility.processNotification(notification)
        }
    }
    fnco.fetchNotificationChangesCompletionBlock = {serverChangeToken, error in

        previousChangeToken = serverChangeToken
        let op = CKMarkNotificationsReadOperation(notificationIDsToMarkRead: readNotificationIDs)
        op.start()
    }
    defaultContainer.addOperation(fnco)
}

最佳答案

我用这个老把戏:

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(fetchPendingNotifications) object:nil];
[self performSelector:@selector(fetchPendingNotifications) withObject:nil afterDelay:2.0f];

关于ios - 如何通过CKFetchNotificationChangesOperation避免无效的网络数据使用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26276728/

10-12 06:06