Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

ios - Completion handler not working as expected in Swift

I have these two functions below, using a completion handler. The questions is highlighted in comments of the 2nd function... why is the result part getting executed even before the asynchronous call in function checforViolationStatus() been completed.

func checkViolationStatus(usr: PFUser, completion: (result: Int32) -> Void) {
    var violations: Int32 = 0
    var query = PFQuery(className: PF_BLOCKEDUSERS_CLASS_NAME)
    query.whereKey(PF_BLOCKEDUSERS_USER, equalTo: usr)


    query.countObjectsInBackgroundWithBlock {
        (count: Int32, error: NSError?) -> Void in
        if error == nil {
            print("Result = (count)")

            //The result here returned is 4, I can see it but always ZERO(0) gets printed in the main function. Unable to understand why.
            violations = count
        }
    }

    completion(result: violations)

}


    func goToMainMenu() {

    if PFUser.currentUser() != nil {

        self.mCould.checkViolationStatus(PFUser.currentUser()!) {
            (result: Int32) in

            //QUESTION: result is getting returned as ZERO even before the actual asynchronous call in the checkforViolation function has been completed - why????

            if result < 4 {
                //Go to Main Menu Screen
                print("result<=4 so calling segue")
                self.performSegueWithIdentifier("segueLoginVCToMainVC", sender: nil)
            } else {
                print("result >=4, so should not be doing anything")
            }

            print("Number of Violations Received Back: (result)")

        }
    }


}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try change your function to this,you should call completion in the countObjectsInBackgroundWithBlock,this method is async.

Or this function return before countObjectsInBackgroundWithBlock is finished

func checkViolationStatus(usr: PFUser, completion: (result: Int32) -> Void) {
var violations: Int32 = 0
var query = PFQuery(className: PF_BLOCKEDUSERS_CLASS_NAME)
query.whereKey(PF_BLOCKEDUSERS_USER, equalTo: usr)


query.countObjectsInBackgroundWithBlock {
    (count: Int32, error: NSError?) -> Void in
    if error == nil {
        print("Result = (count)")

        //The result here returned is 4, I can see it but always ZERO(0) gets printed in the main function. Unable to understand why.
        violations = count
        completion(result: violations)

    }
}
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...