1

在我正在制作的应用程序中,该应用程序将从 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()

                    }


                }

                })

            }}
4

1 回答 1

5

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

 operation.recordFetchedBlock = { record in

当查询准备好时,就会有一个调用。

operation.queryCompletionBlock = { cursor, error in

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

operation.resultsLimit = CKQueryOperationMaximumResults;

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

这是基于以下评论中的链接的示例:

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)
}
于 2015-04-12T18:50:08.073 回答