我如何使我的cloudkit应用程序在加载数据时加载数据? [英] How do i make my cloudkit app load the data as its loading?

查看:94
本文介绍了我如何使我的cloudkit应用程序在加载数据时加载数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我制作的应用程序中,将有很多数据可供应用程序从iCloud加载。我的问题是,直到完成接收所有数据(需要一段时间)后,它才将数据加载到集合视图中。我希望应用程序在接收数据时将数据加载到集合视图中,因此用户不必等待。还是让该应用一次只加载一些数据更好?我该怎么做呢?这是我用来加载数据的代码。

In the app i am making, there will be a lot of data for the app to load from iCloud. My problem is that it does not load the data into a collection view until its finished receiving all the data(which takes a while). I want the app to load the data onto the collection view as its receiving the data, so the user does not have to wait. Or is it better to have the app only load some of the data at a time? How do i do this? Here is my code that i am using to load the data.

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

Note: I am using swift for this project.

  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 。然后,您将获得针对该块的每个记录的调用:

if you do a performQuery, the records will be returned all at once. If you want progress, then you can use the CKQueryOperation. You will then get a call for each record for the block:

 operation.recordFetchedBlock = { record in

查询准备就绪后,就会有一个呼叫。

When the query is ready, then there will be a call to.

operation.queryCompletionBlock = { cursor, error in

中的错误

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

The number of records returned will be limited to a maximum (usually 100). That value can be set by using:

operation.resultsLimit = CKQueryOperationMaximumResults;

只需将其设置为所需的值即可。

Just set it to the value you want.

以下是基于以下注释中的链接的示例:

Here is a sample that is based on the link from the comment below:

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

这篇关于我如何使我的cloudkit应用程序在加载数据时加载数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆