toast toast - 19 days ago 7
iOS Question

Is it okay to segue, without calling a completion handler, if not needed?

Say I have a function that has a completion handler, then calls another function, with a completion handler like this:

func register(withCompletion complete: @escaping (() -> Void)) {
self.sendRegisterRequest(withCompletion: { (error: Error?) -> () in

if error != nil {
self.performSegue(withIdentifier: "ErrorVCSegue", sender: nil)
}
else {
complete()
}
})
}


In the event of an error, it will segue away with calling
complete()
.

Am I ok to segue away like this, without calling
complete()
? I do not need to return from this function as I'm now wanting to go to another View Controller.

Thanks.

Answer

This is a bad idea. A completion handler should be called no matter what. The caller is waiting for a response. It wants to know when it is done. That's the whole point of having a completion handler.

In your case (like many other cases), it would be much better if the completion handler accepted a boolean parameter (and/or an error parameter). This way the completion handler provides some basic information about the success or failure of the method.

Comments