如何对 cloudkit 中给出的结果数量设置限制

Posted

技术标签:

【中文标题】如何对 cloudkit 中给出的结果数量设置限制【英文标题】:How can i set a limit to the number of results given in cloudkit 【发布时间】:2015-04-13 13:48:41 【问题描述】:

这是我从 icloud 检索数据的代码。我如何将它检索到的数据数量限制为 25?我之前看到过这个问题的回答,但我不确定如何将它应用到我的代码中?你能帮忙吗?

这是我之前看到问题的答案。请准确告诉我如何将它添加到我的代码中,或者只是在代码中给出答案。

CKQuery from private zone returns only first 100 CKRecords from in CloudKit

CloudKit Batch Fetches?

import UIKit
import CloudKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate 

    @IBOutlet weak var collectionView: UICollectionView!

    var EventsArray:[String] = [String]()

    var container:CKContainer?
    var privateDatabase:CKDatabase?
    var publicDatabase:CKDatabase?

    override func viewDidLoad() 
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.container = CKContainer.defaultContainer()
        self.privateDatabase = self.container?.privateCloudDatabase
        self.publicDatabase = self.container?.publicCloudDatabase


        self.collectionView.dataSource = self
        self.collectionView.delegate = self

        self.LoadEvents()
    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    

    func LoadEvents() 

            let predicate:NSPredicate = NSPredicate(value: true)
            let query:CKQuery = CKQuery(recordType: "Data", predicate: predicate)
            query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

            if let database = self.publicDatabase 
                database.performQuery(query, inZoneWithID: nil, completionHandler:  (records:[AnyObject]!, error:NSError!) in

                    if error != nil 
                      self.alert("Error: \(error.localizedDescription)", Message: "Make sure iCloud is turned on and you are connected to the internet")
                      self.loading.hidden = true
                    
                    else 
                        dispatch_async(dispatch_get_main_queue()) 

                            self.EventsArray.removeAll(keepCapacity: false)

                            for record in records 
                                let usernameRecord:CKRecord = record as CKRecord
                                if let event = usernameRecord.objectForKey("Events") as? String
                                    self.EventsArray.insert(event, atIndex: 0)
                                
                            

                            //update data
                            self.collectionView.reloadData()
                            self.loading.hidden = true
                        
                    
                    )
                

【问题讨论】:

【参考方案1】:

您应该使用 CKQueryOperation 而不是 performQuery。 然后你可以像这样设置结果限制:

operation.resultsLimit = 25

在这个问题中,您可以看到如何使用 CKQueryOperation:CKQuery from private zone returns only first 100 CKRecords from in CloudKit

【讨论】:

以上是关于如何对 cloudkit 中给出的结果数量设置限制的主要内容,如果未能解决你的问题,请参考以下文章

CloudKit 返回太多记录

如何限制 Sybase 中返回的结果数量?

如何按修改日期对 CloudKit 结果进行排序?

CloudKit 批量获取?

即使正确设置了所有权限,Cloudkit 也会不断给出创建权限错误

了解 CloudKit 限制