使用CloudKit

时间:2017-06-01 04:17:04

标签: ios nspredicate cloudkit ckquery

我在Cloudkit中有一些CKRecords,这些记录有两个int类型的属性,可称为' cost'和'价格',我想查询其费用为' > '价格',但是当我写这样的查询时,应用程序崩溃了:

CKQuery *query = [[CKQuery alloc] initWithRecordType:@"MyRecordType" predicate:
[NSPredicate predicateWithFormat:@"cost > price"]];

这是Xcode提供的崩溃信息:

  

由于未捕获的异常终止应用程序' CKException',原因:'无效的谓词,无效的右表达式,不是函数表达式

请提前帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

行,

更多地考虑这一点。

您查询所有记录,但只选择两个字段;成本和价格。

let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Whatever", predicate: predicate)
let operation = CKQueryOperation(query: query)
operation.desiredKeys = ["cost","price"]

然后你做你的查询[应该更快],整理你想要的记录,然后用publicDB.fetchRecordWithID获取它们。

是的,我知道不止一个查询,事实上它看起来更多,但等待我认为WWDC 2015提示和答案中有答案。 Cloudkit的诀窍;观看会话,你也可以在那里找到一些东西。这里是获取记录的代码,如果你有记录ID。

publicDB.fetchRecordWithID(), completionHandler: {record, error in
if error != nil {
    println("there was an error \(error)")
} else {
    NSOperationQueue.mainQueue().addOperationWithBlock {
        self.plasticOne.text = (record.objectForKey("subCategory") as String)
    }

}    })

抱歉,如果不自己编写代码,我就不能给你更多,如果我这样做,你最终会使用我的答案,我相信你的回答会更好:)