2015-06-04 139 views
0

我正在使用CloudKit在應用程序中工作,並且正在創建訂閱CloudKit。這裏是我的代碼:CloudKit:查詢訂閱請求

CKSubscription *subscription = [[CKSubscription alloc] 
           initWithRecordType:recordType 
           predicate:predicate 
           options:CKSubscriptionOptionsFiresOnRecordCreation | 
           CKSubscriptionOptionsFiresOnRecordUpdate | 
           CKSubscriptionOptionsFiresOnRecordDeletion]; 


CKNotificationInfo *notificationInfo = [CKNotificationInfo new]; 
notificationInfo.shouldSendContentAvailable = YES; 
subscription.notificationInfo = notificationInfo; 
notificationInfo.shouldBadge = YES; 
CKDatabase *publicDatabase = [container publicCloudDatabase]; 
[publicDatabase saveSubscription:subscription 
       completionHandler:^(CKSubscription *subscription, NSError *error) { 
        if (!error) 
        { 
         NSLog(@"subscription success!"); 
        } 
        else 
        { 
         NSLog(@"subscription error%@", error.localizedDescription); 
        } 

       }]; 

我的問題給你們。我如何查詢或驗證用戶訂閱CloudKit?

回答

0

訂閱只不過是在服務器端而不是在您的應用中激活的CKPredicate。如果要驗證謂詞是否正確,那麼只需將其作爲查詢來執行,然後查看返回的結果。

確保您的應用程序difFinishLaunchingWithOptions有以下幾行代碼:

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert | .Badge | .Sound, categories: nil)) 
    application.registerForRemoteNotifications() 

另外,還要確保你加入這個處理收到的通知:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
    NSLog("Push received.. Should be handled..") 
} 
+0

喜@EdwinVermeer。我已經嘗試了您的想法,並且謂詞在tableview中正確工作,但與訂閱無關。我懷疑這是我的訂閱設置,因爲我可以得到一個真正的謂詞來工作。我的謂詞涉及CKReference到當前用戶,在那裏我想獲得其他用戶對當前用戶發表的評論的通知 - let predicate = NSPredicate(format:「user!=%@ AND userIDForPost =%@」,argumentArray :[userRef,userRef])'。 userRef是當前用戶的ID,「user」是對其他用戶的引用,userIDForPost是對當前用戶的引用。超級讚賞任何幫助。 –

+0

我在答案中添加了一些信息。你有嗎?除此之外,你是否在設備上使用iCloud? –

+0

是的,我在AppDelegate中有這些方法。並且該設備使用其他用戶的帳戶登錄到iCloud。然後我進入儀表板並創建新記錄以匹配我自己帳戶下的謂詞,我認爲蘋果公司建議這樣做。過去幾個月我嘗試了很多東西,我開始懷疑這是否是一個錯誤。非常感謝你的幫助! @EdwinVermeer。 –