Returning from threading/ GCD/ completion handler












1















I have some logic to sign a user in from a login screen. If the login fails, I want to display a message to let the user know. If the user logs in successfully, I trigger a segue. When I test it with invalid credentials, the error alert displays as expected but the segue is still being triggered even though it's nested in an if-else statement. Why is this? How can I return out of it and avoid the else block...? I tried adding 'return' under the DispatchQ/ show alert code and that didn't have any effect.



self.login(username: usernameTextField.text!, password: passwordTextField.text!) { (error) in
if error != nil {
DispatchQueue.main.async {
self.showAlert(msg: error ?? "error")
}
} else {
DispatchQueue.main.async {
// segue code
}
}
}


login:



func login(username: String, password: String, completionHandler: @escaping (_ error: String?) -> ()) {
SessionHelper.shared.logUserIn(withUsername: username, andPassword: password) { (error) in
if let err = error {
completionHandler(err)
}
completionHandler(nil)
}
}









share|improve this question

























  • Your sure that’s the only way to trigger the segue? That the callback isn’t being executed twice? From the code you have here it appears it should work as expected which means the problem is most likely elsewhere. Best help would probably be to show login as well.

    – David Berry
    Nov 18 '18 at 2:12











  • @DavidBerry added. Not sure if that helps or you need even more info

    – user7804097
    Nov 18 '18 at 2:25











  • @davidBerry also, yes, positive its the only segue trigger. If I remove it it works as intended - if i add a print statement to the else block, the print runs...

    – user7804097
    Nov 18 '18 at 2:44


















1















I have some logic to sign a user in from a login screen. If the login fails, I want to display a message to let the user know. If the user logs in successfully, I trigger a segue. When I test it with invalid credentials, the error alert displays as expected but the segue is still being triggered even though it's nested in an if-else statement. Why is this? How can I return out of it and avoid the else block...? I tried adding 'return' under the DispatchQ/ show alert code and that didn't have any effect.



self.login(username: usernameTextField.text!, password: passwordTextField.text!) { (error) in
if error != nil {
DispatchQueue.main.async {
self.showAlert(msg: error ?? "error")
}
} else {
DispatchQueue.main.async {
// segue code
}
}
}


login:



func login(username: String, password: String, completionHandler: @escaping (_ error: String?) -> ()) {
SessionHelper.shared.logUserIn(withUsername: username, andPassword: password) { (error) in
if let err = error {
completionHandler(err)
}
completionHandler(nil)
}
}









share|improve this question

























  • Your sure that’s the only way to trigger the segue? That the callback isn’t being executed twice? From the code you have here it appears it should work as expected which means the problem is most likely elsewhere. Best help would probably be to show login as well.

    – David Berry
    Nov 18 '18 at 2:12











  • @DavidBerry added. Not sure if that helps or you need even more info

    – user7804097
    Nov 18 '18 at 2:25











  • @davidBerry also, yes, positive its the only segue trigger. If I remove it it works as intended - if i add a print statement to the else block, the print runs...

    – user7804097
    Nov 18 '18 at 2:44
















1












1








1








I have some logic to sign a user in from a login screen. If the login fails, I want to display a message to let the user know. If the user logs in successfully, I trigger a segue. When I test it with invalid credentials, the error alert displays as expected but the segue is still being triggered even though it's nested in an if-else statement. Why is this? How can I return out of it and avoid the else block...? I tried adding 'return' under the DispatchQ/ show alert code and that didn't have any effect.



self.login(username: usernameTextField.text!, password: passwordTextField.text!) { (error) in
if error != nil {
DispatchQueue.main.async {
self.showAlert(msg: error ?? "error")
}
} else {
DispatchQueue.main.async {
// segue code
}
}
}


login:



func login(username: String, password: String, completionHandler: @escaping (_ error: String?) -> ()) {
SessionHelper.shared.logUserIn(withUsername: username, andPassword: password) { (error) in
if let err = error {
completionHandler(err)
}
completionHandler(nil)
}
}









share|improve this question
















I have some logic to sign a user in from a login screen. If the login fails, I want to display a message to let the user know. If the user logs in successfully, I trigger a segue. When I test it with invalid credentials, the error alert displays as expected but the segue is still being triggered even though it's nested in an if-else statement. Why is this? How can I return out of it and avoid the else block...? I tried adding 'return' under the DispatchQ/ show alert code and that didn't have any effect.



self.login(username: usernameTextField.text!, password: passwordTextField.text!) { (error) in
if error != nil {
DispatchQueue.main.async {
self.showAlert(msg: error ?? "error")
}
} else {
DispatchQueue.main.async {
// segue code
}
}
}


login:



func login(username: String, password: String, completionHandler: @escaping (_ error: String?) -> ()) {
SessionHelper.shared.logUserIn(withUsername: username, andPassword: password) { (error) in
if let err = error {
completionHandler(err)
}
completionHandler(nil)
}
}






swift grand-central-dispatch completionhandler ios-multithreading






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 2:24







user7804097

















asked Nov 18 '18 at 2:06









user7804097user7804097

83110




83110













  • Your sure that’s the only way to trigger the segue? That the callback isn’t being executed twice? From the code you have here it appears it should work as expected which means the problem is most likely elsewhere. Best help would probably be to show login as well.

    – David Berry
    Nov 18 '18 at 2:12











  • @DavidBerry added. Not sure if that helps or you need even more info

    – user7804097
    Nov 18 '18 at 2:25











  • @davidBerry also, yes, positive its the only segue trigger. If I remove it it works as intended - if i add a print statement to the else block, the print runs...

    – user7804097
    Nov 18 '18 at 2:44





















  • Your sure that’s the only way to trigger the segue? That the callback isn’t being executed twice? From the code you have here it appears it should work as expected which means the problem is most likely elsewhere. Best help would probably be to show login as well.

    – David Berry
    Nov 18 '18 at 2:12











  • @DavidBerry added. Not sure if that helps or you need even more info

    – user7804097
    Nov 18 '18 at 2:25











  • @davidBerry also, yes, positive its the only segue trigger. If I remove it it works as intended - if i add a print statement to the else block, the print runs...

    – user7804097
    Nov 18 '18 at 2:44



















Your sure that’s the only way to trigger the segue? That the callback isn’t being executed twice? From the code you have here it appears it should work as expected which means the problem is most likely elsewhere. Best help would probably be to show login as well.

– David Berry
Nov 18 '18 at 2:12





Your sure that’s the only way to trigger the segue? That the callback isn’t being executed twice? From the code you have here it appears it should work as expected which means the problem is most likely elsewhere. Best help would probably be to show login as well.

– David Berry
Nov 18 '18 at 2:12













@DavidBerry added. Not sure if that helps or you need even more info

– user7804097
Nov 18 '18 at 2:25





@DavidBerry added. Not sure if that helps or you need even more info

– user7804097
Nov 18 '18 at 2:25













@davidBerry also, yes, positive its the only segue trigger. If I remove it it works as intended - if i add a print statement to the else block, the print runs...

– user7804097
Nov 18 '18 at 2:44







@davidBerry also, yes, positive its the only segue trigger. If I remove it it works as intended - if i add a print statement to the else block, the print runs...

– user7804097
Nov 18 '18 at 2:44














2 Answers
2






active

oldest

votes


















4














You're running the completion handler either way; if you get an error back, you're calling it, but then you fall through and run it with nil. Try this:



func login(username: String, password: String, completionHandler: @escaping (_ error: String?) -> ()) {
SessionHelper.shared.logUserIn(withUsername: username, andPassword: password) { (error) in
if let err = error {
completionHandler(err)
} else {
completionHandler(nil)
}
}


}






share|improve this answer































    0














    Your login function calls completionHandler twice in the error case. The if falls through to the following statement. You should either put the following statement in an else block, or return from the true block.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357285%2freturning-from-threading-gcd-completion-handler%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      You're running the completion handler either way; if you get an error back, you're calling it, but then you fall through and run it with nil. Try this:



      func login(username: String, password: String, completionHandler: @escaping (_ error: String?) -> ()) {
      SessionHelper.shared.logUserIn(withUsername: username, andPassword: password) { (error) in
      if let err = error {
      completionHandler(err)
      } else {
      completionHandler(nil)
      }
      }


      }






      share|improve this answer




























        4














        You're running the completion handler either way; if you get an error back, you're calling it, but then you fall through and run it with nil. Try this:



        func login(username: String, password: String, completionHandler: @escaping (_ error: String?) -> ()) {
        SessionHelper.shared.logUserIn(withUsername: username, andPassword: password) { (error) in
        if let err = error {
        completionHandler(err)
        } else {
        completionHandler(nil)
        }
        }


        }






        share|improve this answer


























          4












          4








          4







          You're running the completion handler either way; if you get an error back, you're calling it, but then you fall through and run it with nil. Try this:



          func login(username: String, password: String, completionHandler: @escaping (_ error: String?) -> ()) {
          SessionHelper.shared.logUserIn(withUsername: username, andPassword: password) { (error) in
          if let err = error {
          completionHandler(err)
          } else {
          completionHandler(nil)
          }
          }


          }






          share|improve this answer













          You're running the completion handler either way; if you get an error back, you're calling it, but then you fall through and run it with nil. Try this:



          func login(username: String, password: String, completionHandler: @escaping (_ error: String?) -> ()) {
          SessionHelper.shared.logUserIn(withUsername: username, andPassword: password) { (error) in
          if let err = error {
          completionHandler(err)
          } else {
          completionHandler(nil)
          }
          }


          }







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 18 '18 at 3:03









          Ben GottliebBen Gottlieb

          79.4k22168169




          79.4k22168169

























              0














              Your login function calls completionHandler twice in the error case. The if falls through to the following statement. You should either put the following statement in an else block, or return from the true block.






              share|improve this answer




























                0














                Your login function calls completionHandler twice in the error case. The if falls through to the following statement. You should either put the following statement in an else block, or return from the true block.






                share|improve this answer


























                  0












                  0








                  0







                  Your login function calls completionHandler twice in the error case. The if falls through to the following statement. You should either put the following statement in an else block, or return from the true block.






                  share|improve this answer













                  Your login function calls completionHandler twice in the error case. The if falls through to the following statement. You should either put the following statement in an else block, or return from the true block.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 18 '18 at 3:03









                  Ken ThomasesKen Thomases

                  69.6k669106




                  69.6k669106






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357285%2freturning-from-threading-gcd-completion-handler%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Guess what letter conforming each word

                      Port of Spain

                      Run scheduled task as local user group (not BUILTIN)