如何使我的cloudkit应用程序加载数据作为其加载?(How do i make my cloudkit app load the data as its loading?)

编程入门 行业动态 更新时间:2024-10-23 07:29:22
如何使我的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() } } }) }}

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.

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 。 然后,您将收到该块的每条记录的调用:

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

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

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

更多推荐

本文发布于:2023-07-31 00:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340475.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   为其   使我   应用程序   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!