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

Asynchronous call in for loop fires two completions

I have a method to retrieve data. This data can but doesn't have to contain an image. The method below is retrieving the data in order which is crucial for the app: static func getWishes(dataSourceArray: [Wishlist], completion: @escaping (_…
Chris
  • 1,828
  • 6
  • 40
  • 108
-1
votes
1 answer

How to return value within nested async calls?

Assuming "request" is defined and valid func1() // start using "X" func func1() { URLSession.shared.dataTask(with: request) { (data, response, error) in // code, that eventually gets some values "a" and "b" self.func2(a: a, b:…
ooh
  • 9
  • 5
-1
votes
1 answer

Fill an array and then call a function

In completion of a fetch data, I receive a data item one by one. I want to append this item in an array and then call data, how can I do that? how I can tell to the function to wait untill all data append to the array - and the number of data is…
Peter hes
  • 101
  • 6
-1
votes
1 answer

Completion handler for getting coordinates from address

I am having a lot of difficulty wrapping my head around completion handlers. And I have no idea how to use this. Here is the function with the completion handler to obtain geocoordinates based on a string address: func getLocation(from address:…
BVB09
  • 805
  • 9
  • 19
-1
votes
1 answer

How do I upload multiple (different) files to Cloud storage firebase?

I would like to upload multiple files at once to cloud storage. I have an open panel already. I use that to select the files and with the selected files I have an upload file. I thought a ForEach() would work but it either doesn't work or I'm doing…
mathew
  • 29
  • 5
-1
votes
1 answer

Why my completion handler is never called?

I'm trying to call a completion handler but it's never called. I'm doing something wrong ? I see the first NSLog ("Enter in my second funcition"), but the second NSlog ("completion handler") never apears in my console. Here is the definition of the…
Kargol
  • 113
  • 9
-1
votes
2 answers

is this a proper completion handler?

I had a very slow bottom sheet, showing up blank then after a while loading data. I tried to apply a completionHandler isLoadedCompletionHandler, solution worked, but my colleague told me this is not a "completion handler". Could you explain me why…
biggreentree
  • 1,633
  • 3
  • 20
  • 35
-1
votes
1 answer

the code is not entering the completion handler of the data task

The completion handler code is not getting executed. while i debug the code its coming till the session.datatask and after that its not getting into the completion handler. we are actually migrating this project from objectivec to swift. i am not…
-1
votes
4 answers

Execute function after another function is triggered when selecting an item from a tableView

When an item is selected within my tableView, I want the first func, fetchChosenExerciseData, to be executed before the second, goToSegue, is triggered. How can I implement this? I have had a look at completion blocks but to no avail. A snippet of…
gcpreston
  • 135
  • 1
  • 12
-1
votes
3 answers

Variable becomes nil outside completion handler

I am not sure if this is a duplicate or not, I searched and couldn't find an answer. If it is please point me to the similar question. I am setting the value of the imageURL variable inside a completion handler: var imageURL: URL? let options =…
HemOdd
  • 697
  • 1
  • 7
  • 23
-1
votes
1 answer

IOS/Objective-C: Get call back from asynchronous method

At one point in my app, I call a method in a superclass to authenticate with the server. At the end of this method, I would like to run some code specific to the class that calls it. Is this the best way to wait for the response from the…
user6631314
  • 1,751
  • 1
  • 13
  • 44
-1
votes
1 answer

Who passed the arguments to the parameters of the complition handler closure?

func startUpdates(from start: Date, withHandler handler: @escaping CMPedometerHandler) typealias CMPedometerHandler = (CMPedometerData?, Error?) -> Void The above function retrieves the pedometer data from your iOS device. When I called the…
SLN
  • 4,772
  • 2
  • 38
  • 79
-1
votes
3 answers

Xcode requires to put _ before return value name in completion handler

I have a completion handler function with a string in return value. func hardProcessingWithString(input: String, completion: (result: String) -> Void) {} However, Xcode requires me to put _ before my return value name so I have to put _ before…
KevinVuD
  • 581
  • 2
  • 5
  • 25
-1
votes
2 answers

Objective-C completion call never completing

I'm trying to send an instance inside a blocking call and waiting for it finish so I can use the value later on in my program but the completion function never finishes. I'm extremely new to Objective-C and I'm only using objective-c as a wrapper…
GoBig06
  • 255
  • 2
  • 13
-1
votes
1 answer

Can't get data returned from dataTask()

For one week I have been trying to get a string returned from dataTask(). I already read a lot here on StackOverFlow and also from serval sites where they tackle this topic. For example, this one. So I already understand that it's that the dataTask…
Anthony
  • 904
  • 1
  • 8
  • 15