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
5
votes
1 answer

Escaping closure captures non-escaping parameter 'completion' (Swift 5)

In my project, I came across a situation when I need to use the background queue to create an AVPlayerItem (which I create in setupTrackModels function). I'd like do it in getTracks function, and this method must also have a completion handler which…
5
votes
2 answers

How to configure Background App Refresh using Swift?

I have following function to download JSON data in my SeachVC (UIViewController) which works perfect. func downloadJSON(){ guard let url = URL(string: "myURL") else { return } URLSession.shared.dataTask(with: url) { (data, response, err)…
Vetuka
  • 1,523
  • 1
  • 24
  • 40
5
votes
0 answers

Are nested completion blocks a sign of bad design?

I have a method that calls three functions that each make a request to Firebase and pass back data in a completion handler. Once the data from 2 of the completion handlers is sent back, I call another method to pass in the data and send back a valid…
user6354073
5
votes
3 answers

Call Swift completion handler in objective c

I am trying to call a swift method, which is implemented like this:- @objc class DataAPI: NSObject { func makeGet(place:NSString , completionHandler: (String! , Bool!) -> Void) { var str:String = "" let manager =…
Vizllx
  • 9,135
  • 1
  • 41
  • 79
5
votes
2 answers

Cannot convert value of type 'T?' to expected argument type '_?' - Generic types and completion blocks

I'm trying to implement AlamofireObjectMapper (https://github.com/tristanhimmelman/AlamofireObjectMapper) with Alamofire 3 and latest version of ObjectMapper (https://github.com/Hearst-DD/ObjectMapper). It seems that AlamofireObjectMapper, hasn't…
mabril
  • 191
  • 1
  • 7
5
votes
1 answer

How to read a request using CompletionHandlers and a ByteBuffer smaller than the request?

I am using Java 7 and AsynchronousSocketChannel. I would like to read a request (e.g. HTTP POST) but I'm struggling to come up with a nice solution to read the full request, if it's bigger than the size of the ByteBuffer I'm using. E.g. if the…
Jonas
  • 121,568
  • 97
  • 310
  • 388
4
votes
2 answers

SwiftUI - How to call function with completion from view model

I have a form that gets a zip code and passes it to a method. The method looks up the city and state and returns the information as a tuple. The method also has a completion handler so that the rest of the form data isn't saved until the city and…
squarehippo10
  • 1,855
  • 1
  • 15
  • 45
4
votes
3 answers

How to use a completion handler to put an image in a SwiftUI view

I have tried this, but I didn't know how to use the results in a SwiftUI View: func getProfilePicture(_ completion: @escaping ((UIImage) -> Void)) { Alamofire.request(GIDSignIn.sharedInstance()?.currentUser.profile.imageURL(withDimension: 75)…
Eli Front
  • 695
  • 1
  • 8
  • 28
4
votes
6 answers

Completion handler swift 3 return a variable from function

I am confused surrounding the syntax for a completion handler in swift 3. In the function below, after parsing an xml file from a web service call, it should return a variable (an array [String:String]). My attempt is below, but obviously it is…
tim
  • 163
  • 1
  • 2
  • 11
4
votes
1 answer

Accessing PHPhotoLibrary while app is in the background

I have a NSURLDownloadTask which is successfully downloading files in the background (large image or video files). And I successfully copy url's and call this function to save my url to photo library. As you can see, I would like to send a…
NSGangster
  • 2,397
  • 12
  • 22
4
votes
2 answers

Closure with asynchronous method (Swift 2.0)

I'm working on a project that calculates the ETA between various coordinates on a MapView. I use the asynchronous method calculateETAWithCompletionHandler to get the ETA between two coordinates and because calculateETAWithCompletionHandler is…
Ivan
  • 665
  • 2
  • 10
  • 28
4
votes
3 answers

How to have a completion handler/block after Alamofire Post request?

I have a method which handles a Apple Push Notification Service remote notification. When this method is executed, I want it to call my server and do a HTTP POST request using the Alamofire library. I want to execute another method that will handle…
The Nomad
  • 7,155
  • 14
  • 65
  • 100
3
votes
2 answers

Threading implications of AsynchronousByteChannel

The Javadoc for AsynchronousByteChannel.read() says the operation takes place asynchronously, but what happens when the end of stream has been reached? Is an implementation allowed to fire the completion handler within the same thread that invoked…
Gili
  • 86,244
  • 97
  • 390
  • 689
3
votes
1 answer

Swift - Why is DispatchGroup not working in this function?

I am waiting for idToken response before returning the variable. Please don't tell me to just use completion handler and call it without a DispatchGroup. I know I can do that, but I am trying to understand why this logic does not work. func…
Sam KC
  • 61
  • 1
  • 8
3
votes
3 answers

Make multiple network requests on Swift

I am trying to download data from a school course catalog site. I have 64 Urls in the variable UrlBook. I have successfully written code to download a collection of courses and turn them into a single subject object from a single url using…
Jay Gatsby
  • 59
  • 1
  • 3
1 2
3
41 42