简体   繁体   中英

Wait for async function return value in swift

I want to retrieve the user score from Parse and assign it to a variable. This function returns 0 before the query finishes. I have found a similar answer at Retrieve object from parse.com and wait with return until data is retrieved . However, I expect the function have a return value and what argument should I use for the completionhandler when calling this function. Any help would be appreciated, thanks!

Here is my code

func loadCurrentUserData() -> Int {
        let query = PFQuery(className: "userScore")
        let userId = PFUser.currentUser()!
        var currentUserScore: Int = 0

        query.whereKey("user", equalTo: userId)
        query.findObjectsInBackgroundWithBlock {
        (objects: [PFObject]?, error: NSError?) -> Void in
        if error == nil {
            let scoreReceived = objects![0]["score"] as! Int
            currentUserScore = scoreReceived
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.userScore.text = "\(scoreReceived)"
            })
        } else {
            print("Error: \(error!) \(error!.userInfo)")
            }
        }
        return currentUserScore
    }

The way you have set this function will not work as the query method is asynchronous. This can be fixed in two ways:

1) Use the PFQuery synchronous category: http://parse.com/docs/ios/api/Categories/PFQuery(Synchronous).html

The only disadvantage to this approach is that the method will become blocking so make sure to call it from a background thread.

2) Restructure the function to use a completion block instead of a return value..ie:

    func loadCurrentUserData(completion: (score: Int!, error: NSError?) ->()) {
            let query = PFQuery(className: "userScore")
            let userId = PFUser.currentUser()!
            var currentUserScore: Int = 0

            query.whereKey("user", equalTo: userId)
            query.findObjectsInBackgroundWithBlock {
            (objects: [PFObject]?, error: NSError?) -> Void in
            if error == nil {
                let scoreReceived = objects![0]["score"] as! Int
                currentUserScore = scoreReceived
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.userScore.text = "\(scoreReceived)"
                })
                completion(score: currentUserScore, error: nil);
            } else {
                print("Error: \(error!) \(error!.userInfo)")
                }
                completion(score: currentUserScore, error: error);

            }
        }

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