如何使我的cloudkit应用程序加载数据作为其加载?

时间:2015-04-12 15:08:19

标签: ios swift icloud cloudkit icloud-api

在我正在制作的应用程序中,将有大量数据供应用程序从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()

                    }


                }

                })

            }}

1 个答案:

答案 0 :(得分: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)
}