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
-1
votes
1 answer

Swift Closure Completion Handler

I have a function right now that is determining the hide behavior of a UILabel: func shouldHideLabel() -> Bool. I need to retrieve data from a web request to determine whether or not to hide this label, so inside of shouldHideLabel() I'm making a…
Logan
  • 1,172
  • 9
  • 23
-1
votes
1 answer

How to work with async functions swift? Completion handlers

Im trying to wait for the function to process in order to show my image. I have try many things but none of this worked. I know this is an async function and basically i have to wait in order to get the right values but I dont know how to fix this…
-1
votes
2 answers

CompletionHandler with Async call in Swift 3

I would like to return an array (arr) from method (with async call). I implement the completionHandler in the method, but I can't use my method to get my array : Cast from '(@escaping ((Array) -> Void)) -> ()' to unrelated type '[[String :…
Vjardel
  • 1,065
  • 1
  • 13
  • 28
-1
votes
1 answer

Why it does not see my return parameter?

I have a function which requires return parameter: override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! { ... } So it requires…
Doe
  • 431
  • 1
  • 8
  • 16
-1
votes
2 answers

Completion handler with POST request

I have simple login method which returns bool, depends on success of user login. I have problem with order of the responses and execution of the code. I've read about completion handlers, which I think are a solution to my problem but I'm not sure.…
-1
votes
2 answers

Swift 2 OSX How do I use a loop at the end of a series of completions which includes a data task?

I'm using completion blocks for each of my functions (to avoid using a while isDoingSomething loop). I get the expected array when all the blocks complete. But when I try to loop through this final array, it loops continually as expected but…
dbconfession
  • 1,147
  • 2
  • 23
  • 36
-1
votes
1 answer

Swift 2.2 - NSTimer with optional completion-handler selector

I am trying to call a function with optional CompletionHandler parameter from a timer. Below is my code snippet: typealias CompletionHandler = () -> Void class ViewController: UIViewController { override func viewDidLoad() { …
quanguyen
  • 1,443
  • 3
  • 17
  • 29
-1
votes
2 answers

Swift Completion Handler calling Syntax

I have got a massive and time-consuming routine (calculatePower) that needs a completion handler so that it behaves synchronously. The trigger for the request is a UIButton. It is called in: func application(application: UIApplication!,…
Edward Hasted
  • 3,201
  • 8
  • 30
  • 48
-1
votes
1 answer

CompletionHandler is not executed

Im trying to obtain the "locality" from Coordinates using CLGeocoder.reverseGeocodeLocation, but when the execution reach the completionHandler the code inside the bracket is completely skipped to self.currentPlace = (self.place?.locality)! + ", " +…
-1
votes
1 answer

How do I cancel a completion handler?

I want to enhance the code below: when i click the "submitData" button, the added code should cancel the completion handler. func returnUserData(completion:(result:String)->Void){ for index in 1...10000 { print("\(index) times 5 is \(index *…
Vykintas
  • 401
  • 1
  • 8
  • 23
-1
votes
1 answer

Completion handler not working as expected in Swift

I have these two functions below, using a completion handler. The questions is highlighted in comments of the 2nd function... why is the result part getting executed even before the asynchronous call in function checforViolationStatus() been…
user1406716
  • 9,565
  • 22
  • 96
  • 151
-1
votes
1 answer

What is the difference between the below syntaxes of closures in swift

Here is my function declaration with closure func isTextValid(input: String, completion: (result: Bool) -> ()) { if input == "Hello" { completion(result: true) } else { completion(result: false) } } When I am…
Rameswar Prasad
  • 1,331
  • 17
  • 35
-1
votes
2 answers

Change value of properties inside completion block

I am trying to rewrite the AVCam example from Apple in Swift. When I check if the device is authorized I want to set the property deviceAuthorized to true or false. I get inside the block because I get "Access is granted" in my output. But when I…
-2
votes
1 answer

Completion Handler to trigger collectionView Reload once Images are downloaded - Xcode [SWIFT]

I'm using a code I got off here to download some images and present them in a collection view. Heres the code: func downloadImage (url: URL, completion: () -> Void){ let session = URLSession(configuration: .default) let downloadPicTask =…
Dan.code
  • 431
  • 2
  • 14
-2
votes
1 answer

Why is the completion handler in UIAnimation of type ((Bool) -> Void)? And what is the purpose of the bool?

For example UIView.animate(withDuration: 0.5, animations: { self.datePickerView.center.y = self.view.frame.height + (self.datePickerView.frame.height/2) self.datePickerOverlay.alpha = 0.0 }) { (true) in …
14wml
  • 4,048
  • 11
  • 49
  • 97
1 2 3
41
42