我试图在重复循环中运行后台任务( 准确地说是query.findobjectinbackground )。 问题是我需要先完成此操作然后再继续运行循环。
我需要它完成,因为在后台任务中正在填充一个数组,最终将导致填充UITable。
当我以当前状态运行它时,循环会遍历并在后台任务仍在运行时结束。
这是我的代码:
//array Im trying to populate var contentArray:NSMutableArray = NSMutableArray() func queryStatusTable() { contentArray.removeAllObjects() //query my table let queryUser = PFUser.query() let objectIdString = PFUser.currentUser()?.objectId queryUser?.whereKey("objectId", equalTo: objectIdString!) queryUser?.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in if let objects = objects { for object in objects { //used for the repeat loop //object["userFriendsArray"] is an array I got from my above query var i:Int = 0 let count:Int = object["userFriendsArray"].count repeat { //second query let queryFriendStatus = PFQuery(className: "Content") queryFriendStatus.whereKey("userObjectId", equalTo: object["userFriendsArray"][i]) //this is what I want to finish before the loop moves on queryFriendStatus.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in if let objects = objects { for object in objects { self.contentArray.addObject(object["content"]) } } }) //so i dont want this to run until the above task finishes //and putting this inside the above task doesnt work because it will never run if its inside the findobjectinbackground i++ } while (i < count) } } //this is where I reload the table data to populate it with //whats in my *contentArray* self.firstTableView.reloadData() }) }那么我如何确保在self.firstTableView.reloadData()运行之前填充了contentArray ?
任何帮助将被认可,谢谢!
I'm trying to run a background task (query.findobjectinbackground to be exact) inside a repeat loop. The catch is I need this to finish before moving on and running the loop again.
I need it to finish because in the background task an array is being populated that will eventually lead to a UITable being populated.
When I run it in its current state the loop runs through and finishes while the background task is still running.
Here's my code:
//array Im trying to populate var contentArray:NSMutableArray = NSMutableArray() func queryStatusTable() { contentArray.removeAllObjects() //query my table let queryUser = PFUser.query() let objectIdString = PFUser.currentUser()?.objectId queryUser?.whereKey("objectId", equalTo: objectIdString!) queryUser?.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in if let objects = objects { for object in objects { //used for the repeat loop //object["userFriendsArray"] is an array I got from my above query var i:Int = 0 let count:Int = object["userFriendsArray"].count repeat { //second query let queryFriendStatus = PFQuery(className: "Content") queryFriendStatus.whereKey("userObjectId", equalTo: object["userFriendsArray"][i]) //this is what I want to finish before the loop moves on queryFriendStatus.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in if let objects = objects { for object in objects { self.contentArray.addObject(object["content"]) } } }) //so i dont want this to run until the above task finishes //and putting this inside the above task doesnt work because it will never run if its inside the findobjectinbackground i++ } while (i < count) } } //this is where I reload the table data to populate it with //whats in my *contentArray* self.firstTableView.reloadData() }) }So how would I insure that contentArray is populated before self.firstTableView.reloadData() runs?
Any help would be apprectiated, thanks!
最满意答案
为什么不在建立数据时定期更新表格?
它会给用户一些东西看,并会显得快得多。
我之前通过每50行(或其他)调用重载来完成此操作,然后在最后调用它以获取最后一行
记得在后台数据集中通过主队列进行调用 - 就像这样
dispatch_async(dispatch_get_main_queue()) { self.firstTableView.reloadData() }Why not update your table periodically as you build up the data ?
It will give the user something to look at and will appear much faster.
I've done this before by calling the reload every 50 rows (or whatever) and then at the end, call it again to get the last rows
Remember to make the calls through the main queue from within your background data collection - like this
dispatch_async(dispatch_get_main_queue()) { self.firstTableView.reloadData() }更多推荐
发布评论