2016-04-22 104 views
0

我正嘗試使用CloudKit和CKSubcription在幾臺Apple TV之間同步數據。問題是我添加,刪除或更新記錄時從不會調用application:didReceiveRemoteNotification:。我相信我正確配置訂閱,並且我確認他們已添加到CloudKit儀表板。我曾多次嘗試重置儀表板中的開發環境,但這並沒有幫助。我真的不想創建一個定時器來經常抓取每一個。謝謝你的幫助!tvOS中的CloudKit訂閱

此外,我正在使用CloudKit中的私有數據庫,而不是公用數據庫,如果這很重要。

這裏是我的代碼:

AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    CloudKitManager.subscribeToItemUpdates() 
    application.registerForRemoteNotifications() 

    ... 

    return true 
} 

CloudKitManager.swift

class func subscribeToItemUpdates() { 

    if let uuid = UIDevice.currentDevice().identifierForVendor?.UUIDString { 

     saveSubscriptionWithIdentifier(uuid + "create", options: CKSubscriptionOptions.FiresOnRecordCreation) 
     saveSubscriptionWithIdentifier(uuid + "update", options: CKSubscriptionOptions.FiresOnRecordUpdate) 
     saveSubscriptionWithIdentifier(uuid + "delete", options: CKSubscriptionOptions.FiresOnRecordDeletion) 
    } 

} 

class func saveSubscriptionWithIdentifier(identifier: String, options: CKSubscriptionOptions) { 

    let sub = CKSubscription(recordType: "Message", predicate: NSPredicate(value: true), subscriptionID: identifier, options: options) 
    sub.notificationInfo = CKNotificationInfo() 

    let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase 
     publicDatabase.saveSubscription(sub) { (savedSubscription, error) -> Void in 
     if error != nil { 
      print("Error saving CloudKit subscription \(error)") 
     } 
     else { 
      print("Saved subscription to CloudKit", savedSubscription) 
     } 
    } 
} 

回答

0

有tvOS和iOS之間的微小差異。在我的演示應用程序我處理這樣的:

#if os(tvOS) 
//This will only be called when your app is active. So this is what you should use on tvOS 
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
    EVLog("Push received") 
    EVCloudData.publicDB.didReceiveRemoteNotification(userInfo, executeIfNonQuery: { 
     EVLog("Not a CloudKit Query notification.") 
    }, completed: { 
     EVLog("All notifications are processed") 
    }) 
} 
#else 
// Process al notifications even if we are in the background. tvOS will not have this event 
// Make sure you enable background notifications in the app settings. (entitlements: pushnotifications and backgrounds modes - notifications plus background fetch) 
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
    EVLog("Push received") 
    EVCloudData.publicDB.didReceiveRemoteNotification(userInfo, executeIfNonQuery: { 
     EVLog("Not a CloudKit Query notification.") 
    }, completed: { 
     EVLog("All notifications are processed") 
     completionHandler(.NewData) 
    }) 
} 
#endif 

欲瞭解更多信息,請參閱EVCloudKitDao

+0

我使用'應用:didReceiveRemoteNotification'而不是'應用:didReceiveRemoteNotification:fetchCompletionHandler'。我查看了EVCloudKitDao,看起來您正在刪除所有訂閱,並在每次啓動應用程序時重新訂閱。我不這樣做,這可能是問題嗎?有必要嗎?因爲我在使用私有數據庫而不是CloudKit中的公共數據庫,所以它有什麼不同嗎?謝謝 –