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
0
votes
2 answers

Adding a completion handler to my NSURLSession in Swift

I have the following function inside a User class: // Attempts to log in the user, given a password. // Sets user's loginSuccessful to true upon successful login, false otherwise. // If failed login, user's loginFailedMessage is also updated. func…
dulin95
  • 7
  • 1
  • 4
0
votes
2 answers

Completion handler for property in swift

I'm trying to use a control called MZFormSheetController in swift. In the example given it provides a property as a completion handler, if I understand correctly, but I'm having difficulties translating it in Swift. Any help would be…
snksnk
  • 1,566
  • 4
  • 21
  • 46
0
votes
0 answers

Final processing of a loop with multiple completionHandlers

I'm looping over a list of items that each has a method to handle some processing. This method takes in an Action completionHandler that is called when the item in done with its own processing. I need a method called once all of the items in the…
cain
  • 1,028
  • 1
  • 12
  • 26
0
votes
2 answers

Completion Blocks Objective-C

I'm trying to write a completion handler in IOS with a block and am not sure exactly why it's not working. This is what I have so far typedef void(^myCompletion)(BOOL); -(void) showAnswer { [self showAnswer:^(BOOL finished) { if…
Linda Keating
  • 2,215
  • 7
  • 31
  • 63
0
votes
1 answer

Completion Handler called too late

I'm trying to fill a UITableView with data that I acquire via a DataTaskWithURL call. However, the tableview has loaded before the completion handler has started. I searched for this problem and found things about moving it to the main thread but…
Ricardo42
  • 103
  • 1
  • 1
  • 7
0
votes
1 answer

didReceiveRemoteNotification:fetchCompletionHandler: not fetching from server

I want push notifications to get my app to fetch stuff from the server. I use RESTKit. I'm trying to implement application:didReceiveRemoteNotification:fetchCompletionHandler: and then do my REST request (which is a block). I can see in the logs…
bobsacameno
  • 765
  • 3
  • 10
  • 25
0
votes
2 answers

completionHandler and [weak button]

Hy guys, I have these source code let downloadTask = session.downloadTaskWithURL(url, completionHandler: { [weak button] url, response, error in if error == nil && url != nil { if let data = NSData(contentsOfURL: url) { if let…
Alessio Marzoli
  • 167
  • 2
  • 3
  • 10
0
votes
1 answer

Why don't the UITextField and UILabel update when the .text field is changed in a completion block?

I have one view controller and a calculator class. I have an instance of the calculator in the view controller and call a data fetching method through the instance. This also updates variables of the calculator instance. I would like to update a…
badtrader
  • 1
  • 1
  • 1
0
votes
1 answer

Single SKAction Animation on several nodes using a loop with completion handler

Objective: Apply a single SKAction Animation on several nodes using a loop, while waiting for completion handler. func animateShuffle(cards: [Card], completionHandler:(success: Bool) -> Void) { var rotationInRadians:CGFloat = CGFloat(M_PI) …
ericgu
  • 2,229
  • 23
  • 25
0
votes
2 answers

Trying to return a string from a response object inside a completion handler

+ (NSString *) simpleAuth { [SimpleAuth authorize:@"instagram" completion:^(NSDictionary *responseObject, NSError *error) { NSLog(@"plump: %@", responseObject); NSString *accessToken = responseObject[@"credentials"][@"token"]; }]; return…
alemac852
  • 99
  • 1
  • 11
0
votes
1 answer

Sprite Kit runAction delay, weird bug?

this is my third attempt of trying to solve the problem with the delay of runAction with completion. Iv'e done some testing and gotten this far. I hope someone can tell me if there is some setting or other that creates this delay. I started a new…
Znyder
  • 51
  • 7
0
votes
1 answer

Completion Handler Error in Swift

I'm trying to make my own method with completion handler in Swift: func callURL(url: NSURL, username: String, password: String, completionHandler: (NSData?) -> ()?) { let request = NSMutableURLRequest(URL: url) let un = username let pass…
Nikita Zernov
  • 5,465
  • 6
  • 39
  • 70
0
votes
2 answers

calling void after captureStillImageAsynchronouslyFromConnection

I'm trying to run a void saveImageToLibrary when [self.avSnapper captureStillImageAsynchronouslyFromConnection:captureConnection completionHandler:handler]; is done. How would I go about it?
suMi
  • 1,536
  • 1
  • 17
  • 30
0
votes
1 answer

Caching asynchronous responses in iOS

How might I save the response of a completion handler in a static variable? In the sample code GTLQueryTictactoe *query = [GTLQueryTictactoe queryForScoresList]; [service executeQuery:query completionHandler:^(GTLServiceTicket *ticket,…
Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199
0
votes
1 answer

GCD - How to wait on the main thread for an async callback that is performed on the main queue

I want to perform 2 blocks one after the other , where each on there own are performed asynchronously. For instance [someMethodWithCompletionHandler:^() { // do something in the completion handler }]; [anotherMethodWithCompletionHandler:^ { //…
Avba
  • 14,822
  • 20
  • 92
  • 192