阅读背景:

CloudKit中的记录不能超过100条

来源:互联网 

I have tried many different ways to fetch records and checked the same problems on stackoverflow but can't have more than 100 records fetched.

我已经尝试了许多不同的方法来获取记录并在stackoverflow上检查相同的问题但是不能获取超过100条记录。

Before, I was using CKQuery instead of CKQueryOperation to use the resultsLimit parameter.

之前,我使用CKQuery而不是CKQueryOperation来使用resultsLimit参数。

Here is my code:

这是我的代码:

    func initData(){
        // show activity indicator
        self.activityIndicatorView.hidden = false
        // hide connection error button
        self.connectionErrorButton.hidden = true

        let container = CKContainer.defaultContainer()
        let publicDB = container.publicCloudDatabase

        let predicate = NSPredicate(value: true)
        let sort = NSSortDescriptor(key: "id", ascending: true)

        var newInfo = Array<CKRecord>()

        let query = CKQuery(recordType: "info", predicate:  predicate)
        query.sortDescriptors = [sort]

        let queryOperation = CKQueryOperation(query: query)
        queryOperation.queuePriority = .VeryHigh
        queryOperation.resultsLimit = 150;

        // Fetch the pokemons for the record
        func fetched(record: CKRecord!) {
            newInfo.append(record)
        }

        queryOperation.recordFetchedBlock = fetched

        queryOperation.queryCompletionBlock = { [unowned self] (cursor, error) in
            if (error != nil) {
                dispatch_async(dispatch_get_main_queue()) {
                    self.connectionErrorButton.hidden = false
                    self.activityIndicatorView.hidden = true
                    return
                }
            } else {
                dispatch_async(dispatch_get_main_queue()) {
                    self.infos.removeAll()
                    self.infos = newInfo
                    self.tableView.reloadData()
                    self.activityIndicatorView.hidden = true
                    return
                }
            }
        }
        publicDB.addOperation(queryOperation)
    }

1 个解决方案

#1


2  

On iOS 9.3 and below you'll need to use CKQueryCursor to perform multiple fetch operations and keep track of where you were at in the fetch. Thats the cursor object that got passed back in the completion block, you can create another operation with it which will pick up off where you left it like so:

在iOS 9.3及更低版本中,您需要使用CKQueryCursor执行多次获取操作,并跟踪获取中的位置。那是在完成块中传回的光标对象,你可以用它创建另一个操作,它会在你离开的地方拾取它,如下所示:

queryOperation.queryCompletionBlock = { (cursor, error) in
    if let cursor = cursor {
         let cursorOperation = CKQueryOperation(cursor: cursor)
         cursorOperation.queryCompletionBlock = //Handle it again
    }
    //Do stuff
}

Often you'll want to make reusable handler blocks when working with cursors so you can recursively create cursor based query operations if more than one arrives.

通常,您在使用游标时需要创建可重用的处理程序块,以便在多个游标到达时可以递归地创建基于游标的查询操作。


分享到: