2015-03-31 35 views
3

我試圖用一個複合謂詞來使用Cloudkit來做到這一點,但Xcode錯誤說「意外的表達」。任何人都知道我的代碼有什麼問題?感謝任何幫助!NSCompoundPredicate和Cloudkit

let userRef = CKReference(recordID: userID, action: .None) 

    let predicateOne = NSPredicate(format: "recordID IN %@ AND user = %@", postIDs, userRef) 
// I need all user post's that match the IDs in the array and where the user ID matches the Id in the reference field for the user. 

    let predicateTwo = NSPredicate(format: "recordID IN %@", followIDs) 
// Or I want recordID's that match the IDs in this array. 
// I want records if either predicate condition is met, or both, which is why I'm using an OrPredicateType. 

    let predicate = NSCompoundPredicate(type: NSCompoundPredicateType.OrPredicateType, subpredicates: [predicateOne!, predicateTwo!]) 
    let query = CKQuery(recordType: "UserPosts", predicate: predicate) 
    let queryOp = CKQueryOperation(query: query) 
+0

我不明白爲什麼這些謂詞會失敗。你能包括你得到的確切的錯誤嗎? – farktronix 2015-04-01 16:54:37

+0

CKRecordIDs是否有postID和followIDs的CKReferences數組? – Marcus 2015-10-26 21:59:19

+0

@馬庫斯。那些數組是recordID,我正在查詢這些recordID的元數據索引。 userRef變量是一個CKReference。 – 2015-10-26 22:54:26

回答

2

對於使用CloudKit的NSPredicate有一些規則。欲瞭解更多信息,請參閱Apple documentation

什麼,你可以嘗試是創建這樣的斷言:

NSPredicate(format: "recordID IN %@ OR (recordID IN %@ AND user = %@)", followIDs, postIDs, userRef) 

但我有不好的經驗,使用同時安裝了謂詞AND和OR在它的聲明。這個謂詞不起作用,那麼我認爲唯一的解決方案是執行2個單獨的查詢,然後結合結果。你可以通過使用ExSwift庫中的聯合函數來實現這一點。

+0

不幸的是,這沒有奏效,因爲它給出了同樣的錯誤,但感謝您的幫助。我會檢查你提到的聯合功能。 @EdwinVermeer。 – 2015-03-31 20:33:19

+0

你有做多個查詢的最佳做法嗎?我一直在使用第一個查詢中的queryCompletionBlock來調用另一個包含下一個查詢的函數。然後我仍然不確定如果你得到一個遊標該怎麼辦,你不希望第一批記錄處理,直到你運行第二個查詢。我聽說過使用依賴關係。欣賞你的想法。 @EdwinVermeer – 2015-03-31 21:00:40

+0

這通常是我如何做到的。 (從完成塊中調用第二個)。當你想用光標連續執行一個查詢,然後嘗試寫它遞歸(如果光標然後調用自己,否則調用第二個查詢)你可以使用承諾或期貨來簡化這一點。請參閱https://github.com/Thomvis/BrightFutures或https://github.com/ReactKit/SwiftTask除此之外,我當然會使用https://github.com/evermeer/EVCloudKitDao – 2015-04-01 06:27:45