简体   繁体   中英

Letting a background task finish before moving on in a loop

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM