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 call sequence

I have used completionHandler in my Application as follows fun getDetails(completionHandler: (variable: AnyObject) -() ) { // Some work completionHandler(variable) } getDetails { variable in print(variable) } My question is what is the…
-2
votes
1 answer

How to declare this method in swift with a completion block?

I can't find out how to declare the following method in swift: - (void)downloadImageWithURL:(NSURL *)url completionBlock:(void (^)(BOOL succeeded, UIImage *image))completionBlock { NSMutableURLRequest *request = [NSMutableURLRequest…
Sam
  • 1,101
  • 2
  • 13
  • 26
-3
votes
2 answers

How to modify global variables inside closures in swift?

how do I modify a value within a closure or at least get data from a closure in Swift? I have a global variable declared outside a class and I am trying to modify it from within a closure or completion block; however, it is not modified and I cannot…
-4
votes
1 answer

Cannot assign value type '()' to type String?

I am just trying to get some information from a request using completionHandler. The thing is that I need the array with all the information from other part of the code and it seems I can't access to it. Here it is where I access to the…
TibiaZ
  • 730
  • 1
  • 8
  • 21
1 2 3
41
42