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
176
votes
9 answers

How could I create a function with a completion handler in Swift?

I was just curious as to how I would approach this. If I had a function, and I wanted something to happen when it was fully executed, how would I add this into the function? Thanks
traw1233
  • 1,895
  • 2
  • 12
  • 12
80
votes
5 answers

Xcode 8 :function types cannot have argument label breaking my build

It seems that for some reason Swift have chosen to make coding in it less readable by forcing users to remove completion handler parameter labels. I have read the Swift discussion and still think it's a mistake. At least they could have made it…
UKDataGeek
  • 6,338
  • 9
  • 46
  • 63
53
votes
2 answers

Function with optional completion block in Swift

When I create a function, I can make a parameter optional by giving it a default value, like this: func foo(bar: String = "foobar") {} I want to do the same (make it optional) with a completion block. I have tried the following: func…
ntoonio
  • 3,024
  • 4
  • 24
  • 28
27
votes
4 answers

Difference Between Completion Handler and Blocks : [iOS]

I am messed with both completion handler and blocks while I am using them in Swift and Objective-C. And when I am searching blocks in Swift on google it is showing result for completion handler! Can somebody tell me what is the difference between…
shubham mishra
  • 971
  • 1
  • 13
  • 32
26
votes
3 answers

Reloading a UICollectionView using reloadData method returns immediately before reloading data

I need to know when reloading a UICollectionView has completed in order to configure cells afterwards (because I am not the data source for the cells - other wise would have done it already...) I've tried code such as [self.collectionView…
25
votes
2 answers

completionHandler of the 2 methods performFetchWitchCompletionHandler AND didReceiveRemoteNotification interfere each other?

I have a iOS (swift) Application available AppStore since November 2017. I added Firebase Analytics and Crashlytics to proactively see issues which didn't occur during development and testing. I'm currently struggling with the following Stacktrace,…
AlexWoe89
  • 844
  • 1
  • 11
  • 23
18
votes
5 answers

Completion handler is not called on iOS7 GM

I'm using AVAssetWriter, and it is perfectly working on iOS6. The problem is, when I called finishWritingWithCompletionHandler, the completion handler is not called on iOS7 GM. I called markAsFinished, and even endSessionAtSourceTime before I call…
Seho Kim
  • 185
  • 1
  • 6
18
votes
1 answer

How does a completion handler work on iOS?

Im trying to understand completion handlers & blocks. I believe you can use blocks for many deep programming things without completion handlers, but I think i understand that completion handlers are based on blocks. (So basically completion…
marciokoko
  • 4,988
  • 8
  • 51
  • 91
17
votes
2 answers

Completion handlers in Java?

I've been coding for iOS and I'm quite familiar with the concept of blocks in Objective-C. Now I'm leaning Java for Android and trying to convert some apps from Android to iOS. I read that there no perfect equivalent of blocks in Java, so I'd like…
Guilherme
  • 7,839
  • 9
  • 56
  • 99
16
votes
3 answers

Swift 3 :Closure use of non-escaping parameter may allow it to escape

I have the following function where I have completion handler but I'm getting this error: Closure use of non-escaping parameter may allow it to escape Here is my code: func makeRequestcompletion(completion:(_ response:Data, _ error:NSError)->Void) …
user2924482
  • 8,380
  • 23
  • 89
  • 173
14
votes
2 answers

Wait for Firebase to load before returning from a function

I have a simple function loading data from Firebase. func loadFromFireBase() -> Array? { var songArray:Array = [] ref.observe(.value, with: { snapshot in //Load songArray }) if songArray.isEmpty { return…
13
votes
1 answer

Getting data out of completionHandler in Swift in NSURLConnection

I am trying to write a function that will execute an asynchronous GET request, and return the response (as any data type, but here it is as NSData). This question is based on: How to use NSURLConnection completionHandler with swift func…
cosmikwolf
  • 399
  • 7
  • 15
12
votes
2 answers

Dealing with multiple completion handlers

I'm trying to coordinate several completion handlers for each element in an array. The code is essentially this: var results = [String:Int]() func requestData(for identifiers: [String]) { identifiers.forEach { identifier in …
XmasRights
  • 1,427
  • 13
  • 21
11
votes
2 answers

Nested closures does not like argument list

A UIView needs to change a warning label depending on the completion handler of a custom control: voucherInputView.completionHandler = {[weak self] (success: Bool) -> Void in self?.proceedButton.enabled = success …
Ilker Baltaci
  • 11,644
  • 6
  • 63
  • 79
10
votes
3 answers

swift 3 DispatchGroup leave causes crash when called in helper class function

I'm using DispatchGroup.enter() and leave() to process a helper class's reverseG async function. Problem is clear, I'm using mainViewController's object to call mainViewControllers's dispatchGroup.leave() in helper class! Is there a way to do…
1
2 3
41 42