如何让我的 cloudkit 应用在加载时加载数据?

How do i make my cloudkit app load the data as its loading?

在我制作的应用程序中,应用程序需要从 iCloud 加载大量数据。我的问题是它在完成接收所有数据之前不会将数据加载到集合视图中(这需要一段时间)。我希望应用程序在接收数据时将数据加载到集合视图中,这样用户就不必等待。还是让应用程序一次只加载部分数据更好?我该怎么做呢?这是我用来加载数据的代码。

注意:我正在为这个项目使用 swift。

  func loadInfo() {

        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")

                }
                else {



                    dispatch_async(dispatch_get_main_queue()) {

                        self.array.removeAll(keepCapacity: false)

                        for record in records {

                            let usernameRecord:CKRecord = record as CKRecord
                            self.array.append(usernameRecord.objectForKey("Info") as String)





                        }

                        //update data
                        self.collectionView.reloadData()

                    }


                }

                })

            }}

如果执行 performQuery,将一次性返回所有记录。如果你想要进步,那么你可以使用CKQueryOperation。然后,您将收到针对该区块的每条记录的调用:

 operation.recordFetchedBlock = { record in

当查询准备就绪后,就会调用。

operation.queryCompletionBlock = { cursor, error in

返回的记录数将被限制为最大值(通常为 100)。可以使用以下方式设置该值:

operation.resultsLimit = CKQueryOperationMaximumResults;

只需将其设置为您想要的值即可。

这是一个基于以下评论中的 link 的示例:

func loadInfo() {

    let p = NSPredicate(value: true)
    let q = CKQuery(recordType: "Data", predicate: p)
    q.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
    let queryOperation = CKQueryOperation(query: q)
    let database = self.publicDatabase
    self.array.removeAll(keepCapacity: false)

    queryOperation.recordFetchedBlock = fetchedARecord

    queryOperation.queryCompletionBlock = { [weak self] (cursor : CKQueryCursor!, 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")
        }
        else {
        if cursor != nil {
            println("there is more data to fetch")
            let newOperation = CKQueryOperation(cursor: cursor)
            newOperation.recordFetchedBlock = self!.fetchedARecord
            newOperation.queryCompletionBlock = queryOperation.queryCompletionBlock
            database.addOperation(newOperation)
            }
        } else {
            dispatch_async(dispatch_get_main_queue()) {
                self.collectionView.reloadData()
            }
        }

    }

    database.addOperation(queryOperation)
}

var i = 0
func fetchedARecord (record: CKRecord!) {
    println("\(NSDate().timeIntervalSinceReferenceDate*1000) \(++i)")
    self.array.append(record.objectForKey("Info") as String)
}