gpt4 book ai didi

ios - 限制 CloudKit 中返回的结果数量

转载 作者:搜寻专家 更新时间:2023-10-30 21:54:11 25 4
gpt4 key购买 nike

有什么方法可以限制 CKQuery 中返回的结果数量吗?

SQL 中,可以运行类似 SELECT * FROM Posts LIMIT 10,15 的查询。 CloudKit 中是否有类似查询的最后一部分 LIMIT 10,15 的内容?

例如,我想加载前 5 个结果,然后,一旦用户向下滚动,我想加载接下来的 5 个结果,依此类推。在 SQL 中,它将是 LIMIT 0,5,然后是 LIMIT 6,10,依此类推。

一个可行的方法是使用 for 循环,但它会非常密集,因为我必须从 iCloud 中选择所有值,然后循环遍历它们以找出选择哪 5 个,我预计数据库中会有很多不同的帖子,所以我只想加载需要的帖子。

我正在寻找这样的东西:

var limit: NSLimitDescriptor = NSLimitDescriptor(5, 10)
query.limit = limit

CKContainer.defaultContainer().publicCloudDatabase.addOperation(CKQueryOperation(query: query)
//fetch values and return them

最佳答案

您将CKQuery 提交给CKQueryOperationCKQueryOperationcursorresultsLimit 的概念;他们将允许您捆绑查询结果。如文档中所述:

To perform a new search:

1) Initialize a CKQueryOperation object with a CKQuery object containing the search criteria and sorting information for the records you want.

2) Assign a block to the queryCompletionBlock property so that you can process the results and execute the operation.

If the search yields many records, the operation object may deliver a portion of the total results to your blocks immediately, along with a cursor for obtaining the remaining records. If a cursor is provided, use it to initialize and execute a separate CKQueryOperation object when you are ready to process the next batch of results.

3) Optionally configure the return results by specifying values for the resultsLimit and desiredKeys properties.

4) Pass the query operation object to the addOperation: method of the target database to execute the operation against that database.

看起来像:

var q   = CKQuery(/* ... */)
var qop = CKQueryOperation (query: q)

qop.resultsLimit = 5
qop.queryCompletionBlock = { (c:CKQueryCursor!, e:NSError!) -> Void in
if nil != c {
// there is more to do; create another op
var newQop = CKQueryOperation (cursor: c!)
newQop.resultsLimit = qop.resultsLimit
newQop.queryCompletionBlock = qop.queryCompletionBlock

// Hang on to it, if we must
qop = newQop

// submit
....addOperation(qop)
}
}

....addOperation(qop)

关于ios - 限制 CloudKit 中返回的结果数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27728115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com