限制 CloudKit 中返回的结果数量
Limit the amount of results returned in CloudKit
有什么方法可以限制 CKQuery
中返回的结果数量?
在 SQL
中,可以运行类似 SELECT * FROM Posts LIMIT 10,15
的查询。有没有类似查询的最后一部分,LIMIT 10,15
in CloudKit?
例如,我想加载前 5 个结果,然后,一旦用户向下滚动,我想加载接下来的 5 个结果,依此类推。在 SQL 中,它将是 LIMIT 0,5
,然后是 LIMIT 6,10
,依此类推。
一个可行的方法是使用 for
循环,但它会非常密集,因为我必须 select 来自 iCloud 的所有值,然后循环遍历它们找出哪个 5 到 select,我预计数据库中会有很多不同的帖子,所以我只想加载需要的帖子。
我正在寻找这样的东西:
var limit: NSLimitDescriptor = NSLimitDescriptor(5, 10)
query.limit = limit
CKContainer.defaultContainer().publicCloudDatabase.addOperation(CKQueryOperation(query: query)
//fetch values and return them
您将 CKQuery
提交给 CKQueryOperation
。 CKQueryOperation
有 cursor
和 resultsLimit
的概念;他们将允许您捆绑查询结果。如文档中所述:
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)
有什么方法可以限制 CKQuery
中返回的结果数量?
在 SQL
中,可以运行类似 SELECT * FROM Posts LIMIT 10,15
的查询。有没有类似查询的最后一部分,LIMIT 10,15
in CloudKit?
例如,我想加载前 5 个结果,然后,一旦用户向下滚动,我想加载接下来的 5 个结果,依此类推。在 SQL 中,它将是 LIMIT 0,5
,然后是 LIMIT 6,10
,依此类推。
一个可行的方法是使用 for
循环,但它会非常密集,因为我必须 select 来自 iCloud 的所有值,然后循环遍历它们找出哪个 5 到 select,我预计数据库中会有很多不同的帖子,所以我只想加载需要的帖子。
我正在寻找这样的东西:
var limit: NSLimitDescriptor = NSLimitDescriptor(5, 10)
query.limit = limit
CKContainer.defaultContainer().publicCloudDatabase.addOperation(CKQueryOperation(query: query)
//fetch values and return them
您将 CKQuery
提交给 CKQueryOperation
。 CKQueryOperation
有 cursor
和 resultsLimit
的概念;他们将允许您捆绑查询结果。如文档中所述:
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)