Ios 关闭应用程序时的CloudKit订阅

Ios 关闭应用程序时的CloudKit订阅,ios,swift,apple-push-notifications,cloudkit,Ios,Swift,Apple Push Notifications,Cloudkit,我正在使用CloudKit在设备之间同步数据。如果应用程序处于活动状态,一切看起来都很好(数据已更改->使用记录id推送通知->在云和本地coredata数据库中获取/编辑/删除此记录)。但当我按下“主页”按钮时,它就不工作了。 这是我的密码: 首次启动应用程序时创建订阅 func addNewRecordsSub() { let subscription = CKSubscription(recordType: "UserRecords", predicate: p

我正在使用CloudKit在设备之间同步数据。如果应用程序处于活动状态,一切看起来都很好(数据已更改->使用记录id推送通知->在云和本地coredata数据库中获取/编辑/删除此记录)。但当我按下“主页”按钮时,它就不工作了。 这是我的密码:

首次启动应用程序时创建订阅

func addNewRecordsSub() {
    let subscription = CKSubscription(recordType: "UserRecords", predicate: predicate, options: [.FiresOnRecordCreation, .FiresOnRecordUpdate, .FiresOnRecordDeletion])
    let notificationInfo = CKNotificationInfo()
    notificationInfo.shouldBadge = false
    subscription.notificationInfo = notificationInfo
    let privateDatabase = container.privateCloudDatabase
    privateDatabase.saveSubscription(subscription) { subscription, error in
        if error != nil {
            print(error)
        }
        NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
            self.addNewRecordCategoriesSub()
        })
    }
}


func addNewRecordCategoriesSub() {
    let subscription = CKSubscription(recordType: "UserRecordsCategories", predicate: predicate, options: [.FiresOnRecordCreation, .FiresOnRecordUpdate, .FiresOnRecordDeletion])
    let notificationInfo = CKNotificationInfo()
    notificationInfo.shouldBadge = false
    subscription.notificationInfo = notificationInfo
    let privateDatabase = container.privateCloudDatabase
    privateDatabase.saveSubscription(subscription) { subscription, error in
        if error != nil {
            print(error)
        }
        NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
            self.performSegueWithIdentifier("goToRootController", sender: self)
        })
    }
}
AppDelegate

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    UINavigationBar.appearance().barStyle = .Black
    //Push
    let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert]
    let pushNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
    application.registerUserNotificationSettings(pushNotificationSettings)
    application.registerForRemoteNotifications()
    if let options: NSDictionary = launchOptions {
        let remoteNotification = options.objectForKey(UIApplicationLaunchOptionsRemoteNotificationKey) as? NSDictionary
        if let notification = remoteNotification {
            self.application(application,
                didReceiveRemoteNotification: notification as! [NSObject : AnyObject])
        }
    }
    return true
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    print("DEVICE TOKEN = \(deviceToken)")
}


func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    print(error)
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    let ckNotification = CKNotification(fromRemoteNotificationDictionary: userInfo as! [String : NSObject])
    if ckNotification.notificationType == .Query {
        let queryNotification = ckNotification as! CKQueryNotification
        let recordID = queryNotification.recordID!
    //Saving/Update/delete record
}
更新: 好的,我找到了

aps字典还可以包含“内容可用”属性。值为1的content available属性允许远程通知充当静默通知。当静默通知到达时,iOS会在后台唤醒您的应用程序,以便您可以从服务器获取新数据或进行后台信息处理。用户不会被告知由静默通知产生的新信息或更改信息,但他们可以在下次打开应用程序时发现

对于无声通知,请注意确保aps字典中没有警报、声音或徽章负载。如果您不遵循此指南,则配置不正确的通知可能会被阻止,而不会在后台传递到应用程序,并向用户显示而不是静默

现在它的工作原理是:按主页按钮->再次打开应用程序->获取新数据

let subscription = CKSubscription(recordType: "UserRecords", predicate: self.predicate, options: [.FiresOnRecordCreation, .FiresOnRecordUpdate, .FiresOnRecordDeletion])
    let notificationInfo = CKNotificationInfo()
    notificationInfo.shouldBadge = false
    notificationInfo.shouldSendContentAvailable = true
    subscription.notificationInfo = notificationInfo
    let privateDatabase = self.container.privateCloudDatabase
    privateDatabase.saveSubscription(subscription) { subscription, error in
        if error != nil {
            print(error)
        }
        NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
            self.addNewRecordCategoriesSub()
        })
    }

但我看到了另一个问题。如果我关闭应用程序(双击主页并滑动),我将无法获取数据。是我还是iOS不支持它?

苹果文档在几个地方声明,如果完全关闭应用程序,它将停止在后台运行。你应该重新教育那些仍在强制关闭应用程序的用户(几年前,这是一个骗局,认为这是一个好主意,自那以后就被广泛揭穿,因为iOS应该以最有效的方式管理自己的内存。)