如何让我的cloudkit应用程序加载的数据作为其加载?(How do i make my clou

2019-10-22 14:51发布

在应用程序,我想提出,会有大量数据的应用程序从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()

                    }


                }

                })

            }}

Answer 1:

如果你做一个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)
}


文章来源: How do i make my cloudkit app load the data as its loading?