Описание тега 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: