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

completionHandler - Type of expression is ambiguous without more context

I've following code, I'm getting error for completionHandler Type of expression is ambiguous without more context I understand that it's due to mismatch for completionHandler. How do I return a string/meaningful details in error for…
Raymond
  • 1,108
  • 13
  • 32
2
votes
1 answer

Calling completion handler within nested closure to stop recursive function

My program pulls from an API to return appointment data into a widget. I set up a closure to handle pulling an unknown number of pages from the API. Each time loadResults is called, it queries the API, starting with page 1, and then checks to see…
2
votes
2 answers

How to use results from Swift completion handler?

I'm new to Swift and SwiftUI. In my macOS SwiftUI project, I'm trying to verify that a URL is reachable so I can present one of two views conditionally. One view which loads the image URL, another one which displays an error image if the URL is not…
Edward J. Stembler
  • 1,932
  • 4
  • 30
  • 53
2
votes
1 answer

Completion handler returning before task finishes

I have this function that has a completion handler that is to be returned after the task is finished, but from the output I am getting, it shows that the completion handler is being considered completed and returned before the task is done. Function…
2
votes
1 answer

How to wait for the completion of the audio in SwiftUI?

All I want is to wait until the sound is finished and call the next sound... This is main function in the ConentView struct I'm using to play the sounds with handlers: func askNew(){ self.isPlaying = true playQuestion{ …
J A S K I E R
  • 1,976
  • 3
  • 24
  • 42
2
votes
2 answers

Difference between two method calls

Given the following definition: func retrieveData(for id: Int, completion: @escaping (Record) -> ()) What is the difference between the following two calls to retrieveData? first call: let id: Int = 1 retrieveData(id: id) { self.update(with:…
Asif Newaz
  • 557
  • 7
  • 17
2
votes
1 answer

Is there an easy way in Xcode to remove the completion block from the end?

Every Swift function with a completion handler on the end like so: func hello(_ name: String, completionHandler: (String) -> Void) { completionHandler("Hello \(name).") } Can be called like: hello("Stack Overflow", completionHandler: { text in …
Mark
  • 16,906
  • 20
  • 84
  • 117
2
votes
2 answers

Completion handlers and Operation queues

I am trying to do the following approach, let operationQueue = OperationQueue() operationQueue.maxConcurrentOperationCount = 10 func registerUser(completionHandler: @escaping (Result) -> Void) -> String { self.registerClient()…
Perseus
  • 1,546
  • 4
  • 30
  • 55
2
votes
2 answers

How to call function after async requests finish in SwiftUI?

I have a function that calls 2 types of api requests to get a bunch of data i need in my app. In the function I make a request for locations, then for each location in the response I make a different request to get details of that specific location.…
zlyt
  • 259
  • 5
  • 23
2
votes
0 answers

CATransaction completion block is called twice

I am trying to make CABasicAnimation with a CATransaction, so I can have a Completion handler. It works fine but when I quit the view Controller and come back, the completion block is called twice : Once when the animation starts and once it…
2
votes
2 answers

Calling completion handler of a function from Timer selector function Swift

I have animateButtons() function of which I need to set completion handler only when the animation has finished. The problem is that in animateButtons() I can only set the completion handler just after imageView.startAnimating() so the whole timing…
Vincenzo
  • 5,304
  • 5
  • 38
  • 96
2
votes
1 answer

Is there a way to have a function call itself without re-specifying all its arguments?

If anyone can think of another way to achieve what I'm trying to do, your thoughts are welcome! To give you an idea, I have over 40 different functions that have signatures similar to these: func getXFromServer(arg1: String, arg2: Int, arg3: Bool,…
Merricat
  • 2,583
  • 1
  • 19
  • 27
2
votes
3 answers

What is the best way to write a completion handler

I am currently working on implementing In App Purchases in my app and after restoring purchases i would like to call a completion to perform an action of displaying an alert to the user. I was doing it this way and found a post that says it might…
mandem112
  • 181
  • 14
2
votes
1 answer

how to use the return value in a completion handler?

I'm trying to understand better the closures in swift and I've tried to create a silly function with a completion handler that returns a value but this value goes nowhere... here's what I did.. func productToString(num: Int,num2: Int,completion:…
Lucas
  • 746
  • 8
  • 23
2
votes
2 answers

`UIViewController.present` completion handler being called immediately

I am using Xcode 10 Beta 6, so this might just be a bug. I am trying to present a view controller (colorPickerController) as a popover. Within that view controller I will be able to set some properties, which I want to read once the popover is…