Questions tagged [completionhandler]

Completion handlers are blocks of code provided to an asynchronous function, whereby the calling function can supply code to be performed by the asynchronous function upon completion of its asynchronous task.

Completion handlers are blocks of code provided to an asynchronous function, whereby the calling function can supply code to be performed by the asynchronous function upon completion of its asynchronous task.

Asynchronous functions are those that initiate some some potentially time-consuming task, but may return before that task is done (often returning very quickly after initiating the asynchronous task). A completion handler is often provided by these asynchronous functions to provide a simple mechanism for the the caller to supply some block of code that should be performed upon completion of the asynchronous task. This provides a simple mechanism for the calling function to specify what other operations should be performed when the asynchronous task is done (e.g. update UI, update model, etc.).

This completion handler pattern is just one of many different ways that asynchronous functions can notify calling function of the completion of some asynchronous tasks. Those other mechanisms include delegates, notifications, semaphores, etc. The completion handler pattern is arguably one of the more concise techniques to inform a caller of the completion of an asynchronous task.

Different languages use different mechanisms to supply completion handlers:

  • In Swift, completion handlers are defined by closures.
  • In Objective-C, completion handlers are implemented using blocks.

Many other languages use similar constructs, called anonymous functions or lambda abstractions for similar purposes. Note, completion handlers is only one example of the use of these constructs.

Related tags:

619 questions
2
votes
1 answer

Swift Firebase Check if user exists

What am i doing wrong? I have a database structure like the one shown in this image. In appleDelegate.swift i just want to check if a certain user token actually exists under the "users" node. that is, if "users" has the child currentUserID (a…
2
votes
4 answers

Wait for two completion handlers?

Inside my app, I do few requests to my back-end. In one case I have a function that should return two parameters (may be Bool and Bool for example). But there comes my problem. To return those two parameters, I need to receive some info from…
mcgtrt
  • 657
  • 6
  • 22
2
votes
4 answers

Grand Central Dispatch for complex flow?

I have a, b, c, d, e time consuming task functions with completion handler. There are constraints between them: Both b & c wait for a to finish The last task e waits for b & c & d to finish if there is no task d, I could write code in swift like…
2
votes
1 answer

get value passed to completion handler for unit testing

I'm trying to get the value passed to a completion handler to test against for unit testing. It is not a normal setup for a completion handler since the completion handler is a var: typealias ResultType = Result typealias CompletionType…
SwiftyJD
  • 5,257
  • 7
  • 41
  • 92
2
votes
0 answers

Preform segue inside closure

I am trying to display the value of btc in a separate view controller but EthViewController is not changing if I set the label equal to btc inside of a closure. func btcValue(completion: @escaping((String) -> ())){ //Added Line let url =…
2
votes
2 answers

How do I use completion handlers in Fire Base

I have trouble understanding how to properly using completion handlers, especially in relation to firebase code. As I understand it, the completion handler executes when the Fire Base has done it's thing asynchronously, right. So just to get a feel…
tore
  • 619
  • 2
  • 9
  • 23
2
votes
1 answer

Kotlin completion with multiple parameters

Imagine a method with a multiple parameter completion: fun foo(completion : (p1 : Int, p2 : Int) -> Unit){ completion(1, 2) } How to access p1 and p2 when calling foo?
Gustavo Vollbrecht
  • 3,188
  • 2
  • 19
  • 37
2
votes
4 answers

Swift: What does "completionHandler: ((Bool) -> Void)" mean?

Can someone explain what completionHandler: ((Bool) -> Void) means? For instance it appears when requesting camera access: AVCaptureDevice.requestAccess(for: AVMediaType.depthData, completionHandler: (<#T##(Bool) -> Void#>)) I usually do this to…
RjC
  • 827
  • 2
  • 14
  • 33
2
votes
2 answers

How to get out data from completion handler

I am trying to get a function that calls an API via HTTP request and return a String. My request function has a completion handler that returns the Data and it is used by my function. func buildHTML () -> String { var htmlString: String = "" …
Oswaldo Rubio
  • 50
  • 2
  • 9
2
votes
1 answer

NSURLSession Download Task - Xamarin iOS F#

I'm trying to implement the following method from the C# Xamarin.IOS in F#: NSUrlSession.SharedSession.CreateDownloadTask(Request, (data, response, error) => { if(response == null) { Client.FailedWithError(this,…
Alk
  • 5,215
  • 8
  • 47
  • 116
2
votes
1 answer

Swift: URLSession Completion Handlers

I am trying to get some data from a local server, using a piece of code which worked in an Xcode playground file: URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) -> Void in if let jsonObj =…
jasperthedog
  • 129
  • 3
  • 9
2
votes
2 answers

How to have only 1 completion handler for 2 Alamofire request in the same function

I have a function with 2 Alamofire requests inside with a completion handler. I want to reload my collectionView once I have both requests completed downloading data from server. I have a completion handler in the function but it gets called twice…
user8896788
2
votes
1 answer

Completion Handler dispatch_async is giving error?

I have completion handler in following function static func fetchFeatureApp(completionHandler:@escaping ([AppCategory1])->()){ let urlString="http://ebmacs.net/ubereats/Api/all_product?id=1" let url = URL(string: urlString)! …
user8298737
2
votes
1 answer

Swift: Multithreading with Completion Handler

I have a function that accesses a critical variable and needs mutually exclusive access to it, but returns via completion handler. This is what I am currently using to attempt this: static func getAccessTokenValue(completionHandlerResult : @escaping…
2
votes
3 answers

Pass function to completion handler

I have a function that performs an animation on a view. I would like to implement a completion handler for this function that would be called after the animation is complete. In ViewController... hudView.hide(animated: true, myCompletionHandler: { …
Turnip
  • 35,836
  • 15
  • 89
  • 111