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
3
votes
3 answers

Expected Type Before / After —>, Expected Declaration in Swift 3

So, I created a typealias to store a completion handler, to later pass into a function called submitTokenToBackend using Stripe's iOS library. Here is my code: // MARK: - Create Completion Handlers typealias CompletionHandler = (_ token:…
user6684504
3
votes
2 answers

How To Check If An Email Address Is Already In Use Firebase

working on some FirAuth things, but for some reason I can't figure out how to check if the users proposed email address has already been taken. I've tried calling .fetchProvidersForEmail as suggested in other questions from a while back, but for…
Ethan
  • 1,905
  • 2
  • 21
  • 50
3
votes
2 answers

Confusing closures and completion handles

Im a new programmer and am very lost. I am taking this online iOS dev course and I was configuring collection view cell. However, closures and completion handles were used and it was never mentioned before. import UIKit class PersonCell:…
JasonP
  • 1,575
  • 3
  • 12
  • 9
3
votes
1 answer

How can I make a function complete before calling others in an IBAction?

I'm having trouble understanding completion handlers. I have a textFieldEditingDidChange IBAction that calls first a verify() function on the textfield input and then an if statement on the boolean returned by apply. The problem is that the if…
standousset
  • 1,092
  • 1
  • 10
  • 25
3
votes
2 answers

How to chain completions?

What is the best way to chain completions in Swift? I'm using network library that fetches some information from API. To fetch information I need: Authorize -> Get list of categories -> Get list of forums -> Get list of topic -> Nested completions…
Vasily
  • 3,740
  • 3
  • 27
  • 61
3
votes
2 answers

JSON is not convertible to void (Openweather map API)

I am calling Openweather map API using Swift and from the response I need to return a particular value as string. However when I try to return the value error comes as JSON is not convertible to string. func callWeatherServ(name:String,…
RPP
  • 55
  • 6
3
votes
4 answers

Completion Handlers in Swift

I'm fairly new at swift development and trying to get a handle on closures and completion handlers. I have a function with the following declaration inside a struct called ObjectData func getData(id1:Int, id2:Int, completion: (dataObject? -> Void))…
c11ada
  • 4,302
  • 15
  • 48
  • 62
3
votes
3 answers

Why NSAnimationContext completionHandler does not work (sometimes)?

// wc here is an NSWindowController [NSAnimationContext beginGrouping]; [[NSAnimationContext currentContext] setDuration:0.5f]; if (duplication) { NSPoint origin = initialSize.origin; origin.y +=…
Hofi
  • 945
  • 1
  • 6
  • 18
3
votes
2 answers

What makes a completion handler execute the block when your task of interest is complete?

I have been asking and trying to understand how completion handlers work. Ive used quite a few and I've read many tutorials. i will post the one I use here, but I want to be able to create my own without using someone else's code as a reference. I…
marciokoko
  • 4,988
  • 8
  • 51
  • 91
3
votes
0 answers

iOS 7 Completion handler never gets called

In the following code none of the completion handlers ever get executed. The one explanation I was able to find is this Bug in iPhone Simulator 5.1 with Xcode 4.5 using UIManagedDocument . It says that it might be a bug in iPhone simulator 5.1 .…
2
votes
0 answers

How to parse BLE notifications with CoreBluetooth and completionHandler in Swift?

I have to add a BLE device support in my simple project written in Swift UI. Model: Phone sends a request (.withoutResponse) <-> BLE Device answers by BLE notification. As I am not a professional Swift programmer my main problem is how to parse…
Tim
  • 21
  • 1
2
votes
1 answer

How do you convert a library function with two completion handlers into an async function?

I need to wrap a function as below from a library into an async function. The error on my First Trial as below is: Missing argument for parameter 'errorHandler' in call. How can I wrap it properly? Your comments will be appreciated. Original…
2
votes
1 answer

How to make API call in swiftUI

I am trying to use PokeApi to make a Pokedex app. I just started swift a couple of days ago so I'm following a tutorial here: https://www.youtube.com/watch?v=UsO-84Xnhww. The tutorial doesn't seem to work, and I don't know how to access the PokeAPI…
2
votes
4 answers

Swift CLGeocoder to get TimeZone

I'm afraid I likely have the completion handler all messed up. What I am trying to do is use latitude and longitude to get a TimeZone. I want the entire function to return the value with a type TimeZone. The following code works, as in I can get the…
AvsBest
  • 435
  • 1
  • 4
  • 9
2
votes
1 answer

FileManager .removeItem(at:) takes long – how do I know when it's done?

My users are able to store files locally in the Documents/ folder. Later, they can delete those files from a list. I delete the files like this: List { ForEach(urls, id: \.self) { url in Text(url.lastPathComponent) } .onDelete {…
LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174