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

Swift Completion Handler For Loop to be performed once instead of 10 times due to the loop

I I have a loop with a firestore query in it that is repeated 10 times. I need to call the (completion: block) after all the 10 queries completed; Here I have my code so that it performs the (completion: block) per query but this would be too heavy…
3
votes
3 answers

iOS/Swift - What is the difference between Closure/Completion blocks and Delegates/functions?

I don't clear about these two, Nowadays the world is shifting to the closure types. But I'm not clearly understanding this. Can someone explain me with a real-time example?
Surezz
  • 561
  • 4
  • 12
3
votes
2 answers

synchronous NSURLSessionDataTask using objective-c

I'm try to do synchronous NSURLSessionDataTask with the below code but unable to proceed. __block NSData *rData = nil; __block BOOL taskDone = NO; __block NSData *rError = nil; NSURL *url = [NSURL URLWithString:dataURL]; …
sia
  • 1,872
  • 1
  • 23
  • 54
3
votes
2 answers

What are the effects of never calling a function's completion handler?

What happens if you never call the completion handler? Are there any side effects? I am assuming there might be a memory leak, but I cannot find any documentation. func completeMe(completionHandler: @escaping (String?) -> Void) { return }
Mocha
  • 2,035
  • 12
  • 29
3
votes
1 answer

If an SKAction is removed, is the completion still run?

As in the title, if I remove an action using sprite.removeAllActions(), does the action's completion still get run? Here is a basic code snippet to help show what I am asking: import SpriteKit /// The action to add let action = SKAction.moveBy(x:…
George
  • 25,988
  • 10
  • 79
  • 133
3
votes
1 answer

Grab HTML from WKWebView using evaluateJavascript and then store it in a variable (using Swift)

I have users logging into a website using a WKWebView after which I want to parse the HTML of the page. I am very much new to Swift/iOS Development (just learning things as I go along). I know the id of the HTML tag I am trying to grab the innerHTML…
3
votes
1 answer

Swift Completion Handler for mapView.setRegion Animation?

I'm trying to figure out how to tell when the animation is finished for the following: self.mapView.setRegion(MKCoordinateRegionForMapRect(mapRect), animated: true) It doesn't look like setRegion supports a completion handler like other commands. I…
Joe
  • 131
  • 10
3
votes
2 answers

swift - how to return from a within a completion handler closure of a system function?

I know this can't work because the completion handler is on a background Thread but where am I supposed do dispatch the main queue or what else do i have to do? this is the code: static func isNotificationNotDetermined() -> Bool{ var…
3
votes
1 answer

Return HTML string from the completion handler of the evaluateJavaScript function

I know that I'm not the first one to ask this but I can't solve the problem. I'm trying to take a piece of string from HTML using evaluateJavaScript in Xcode with Swift 3 and the piece of text is called value inside the completion handler, so I did…
Albifer
  • 167
  • 2
  • 10
3
votes
1 answer

Return String from URLSession Completion Handler function Swift 3

I need this function (which is getting some data from a database) to return a string but I'm having trouble figuring out how. I've found some similar questions/solutions but I'm still struggling to implement a proper solution (I'm pretty new with…
m00912
  • 31
  • 2
3
votes
2 answers

Safe way to download content from a list of URL's in Swift 3

I have a list of URL's that provide a location to an Image resource. I managed to find a solution, but I feel as if there is a much better way than the procedure shown in the code below. Any tips on how to improve this method to retrieve images…
user6354073
3
votes
1 answer

objective c completion handler

I have RequestManager class with getContentInBackgroundWithMemberId and postRequest functions. I want to call them from my view controller and get result using completion handler. How to edit my functions? RequestManager.h #import…
Denis Windover
  • 445
  • 2
  • 6
  • 20
3
votes
1 answer

Access variable in Firebase closure

I have the following Firebase query where I check if a username already exists in a database. I would like to assign a boolean value (true) to a variable called usernameAlreadyExists. However, I need to access this variable with its new value…
user6776610
3
votes
4 answers

Syntax to wrap a completion handler in dispatch async

I have a completion handler that I need to assign to a property, but I want it to execute asynchronously. If I didn't have that requirement, I would write: request.completionBlock = completionBlock But since I have this requirement, I have to…
KPM
  • 10,558
  • 3
  • 45
  • 66
3
votes
2 answers

Different types of closure syntax in swift - which one is correct?

I'm pretty curious which one of these syntax statements is (more) correct. Playground happily compiles both cases. Method 1 // copied from SO and this appears clear to me UIView.animate( withDuration: 3.0, animations: { }, …
GiorgioE
  • 204
  • 2
  • 10