问题描述
我想从CloudKit获取公共/私人条目的最后X分钟。
I want to fetch the last X minutes of public/private entries from CloudKit.
我尝试了这个效果但失败了:
I tried something in this effect but failed:
let date = NSDate(timeInterval: -60.0 * 120, sinceDate: NSDate())
let predicate = NSPredicate(format: "creationDate > %@", date)
但这会得到我的数据,但我不确定我是不是查询所有内容或仅查询某种上限:
But this will get me data, but I'm not sure if I'm querying everything or just to some kind of cap:
let predicate = NSPredicate(value: true)
我希望能够在一定的时间内查询。如果没有在客户端进行创建排序逻辑,这是否可行?
I want to be able to query by certain amount of time. Is this possible without doing the creation sorting logic on the client side?
这是完整的代码块:
func fetchPublicData(completion: ((records:[AnyObject]) -> Void)!)
{
let date = NSDate(timeInterval: -60.0 * 120, sinceDate: NSDate())
let predicate = NSPredicate(format: "creationDate > %@", date)
let query = CKQuery(recordType: "MyDataRecordType", predicate: predicate)
let container = CKContainer.defaultContainer()
let publicDb = container.publicCloudDatabase
publicDb.performQuery(query, inZoneWithID: nil,
{
(results, error) in
if error != nil
{
self.handleError(error)
}
else
{
// do stuff
}
})
}
谢谢。
推荐答案
找到我的o问题。
您需要登录CloudKit仪表板并选中此复选标记框。
Found out my own question.You'll need to log in to CloudKit dashboard and have this checkmark box checked.
一旦选中,您就可以按日期查询:
Once it's checked, you'll be able to query by date like so:
let date = NSDate(timeInterval: -60.0 * 120, sinceDate: NSDate())
let predicate = NSPredicate(format: "creationDate > %@", date)
这里有更详细的文件说明你可以使用哪种NSPredicate用于CloudKit:
Here's more detailed document of what kind of NSPredicate you can use for CloudKit:https://developer.apple.com/documentation/cloudkit/ckquery#//apple_ref/doc/uid/TP40014043-CH1-SW8
这篇关于如何在CloudKit中通过creationDate进行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!