Returning from threading/ GCD/ completion handler
up vote
1
down vote
favorite
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
add a comment |
up vote
1
down vote
favorite
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
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 at 2:12
@DavidBerry added. Not sure if that helps or you need even more info
– user7804097
Nov 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 at 2:44
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
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
swift grand-central-dispatch completionhandler ios-multithreading
edited Nov 18 at 2:24
asked Nov 18 at 2:06
user7804097
71110
71110
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 at 2:12
@DavidBerry added. Not sure if that helps or you need even more info
– user7804097
Nov 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 at 2:44
add a comment |
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 at 2:12
@DavidBerry added. Not sure if that helps or you need even more info
– user7804097
Nov 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 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 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 at 2:12
@DavidBerry added. Not sure if that helps or you need even more info
– user7804097
Nov 18 at 2:25
@DavidBerry added. Not sure if that helps or you need even more info
– user7804097
Nov 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 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 at 2:44
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
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)
}
}
}
add a comment |
up vote
0
down vote
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.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
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)
}
}
}
add a comment |
up vote
3
down vote
accepted
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)
}
}
}
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
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)
}
}
}
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)
}
}
}
answered Nov 18 at 3:03
Ben Gottlieb
79.1k22168169
79.1k22168169
add a comment |
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
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.
answered Nov 18 at 3:03
Ken Thomases
68.5k668104
68.5k668104
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 at 2:12
@DavidBerry added. Not sure if that helps or you need even more info
– user7804097
Nov 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 at 2:44