2016-04-22 9 views
0

Ich versuche, Daten zwischen einigen Apple TVs mit CloudKit und CKSubcription zu synchronisieren. Das Problem ist application:didReceiveRemoteNotification: wird nie aufgerufen, wenn ich Datensätze hinzufügen, löschen oder aktualisieren. Ich glaube, ich konfiguriere die Abonnements korrekt und ich habe bestätigt, dass sie dem CloudKit-Dashboard hinzugefügt werden. Ich habe wiederholt versucht, die Entwicklungsumgebung im Dashboard zurückzusetzen, aber das hilft nicht. Ich möchte wirklich keinen Timer erstellen, der so oft abzurufen ist. Danke für jede Hilfe!CloudKit Abonnements in tvOS

Ich verwende auch die private Datenbank in CloudKit und nicht die öffentliche Datenbank, wenn das wichtig ist.

Hier ist mein Code:

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) 
     } 
    } 
} 

Antwort

0

Es gibt einen kleinen Unterschied zwischen tvOS und iOS. In meinem Demo-Anwendung behandeln ich es wie folgt aus:

#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 

Weitere Informationen finden Sie EVCloudKitDao

+0

Ich benutze 'Anwendung: didReceiveRemoteNotification' und nicht' Anwendung: didReceiveRemoteNotification: fetchCompletionHandler'. Ich habe EVCloudKitDao angeschaut und es sieht so aus, als ob Sie bei jedem Start der App alle Abonnements löschen und neu abonnieren. Ich mache das nicht, ist das möglicherweise das Problem? Ist es nötig? Macht es einen Unterschied, da ich die private Datenbank und nicht die öffentliche Datenbank in CloudKit verwende? Vielen Dank –