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

How can I prevent nested completion blocks in Swift 3?

I have a series of nested completion blocks in the code provided below. This is because I need to make separate network requests in the background to abstract data to be used in the next method, which provides another completion block, and so on. Is…
user6354073
10
votes
8 answers

UIActivityViewController completion handler still calls action if user presses cancel

In my UIActivityViewController, I use completion handler to execute a "successfully shared" notification. It works but my only problem is, it still shows the notification if the user presses cancel. Here is my completion handler code, [controller…
Colton Anglin
  • 431
  • 3
  • 9
  • 19
9
votes
4 answers

Wait for completion handler to finish - Swift

I am trying to check if UserNotifications are enabled and if not I want to throw an alert. So I have a function checkAvailability which checks multiple things, including the UserNotification authorization status. func checkAvailabilty() -> Bool { …
Codey
  • 1,131
  • 2
  • 15
  • 34
9
votes
6 answers

Completion Handler is not working in viewDidLoad?

I'm using this library in my app for banners. I am trying to get the Link by parsing the JSON. The Images are are not showing in the slideshow view. If I press the slideshow view, after that everything works fine. I thought that there was some…
Arulnadhan
  • 923
  • 4
  • 17
  • 46
8
votes
1 answer

Swift Custom UIAlertView

I'm trying to make a confirm deletion popup view. Because the design I want is very different from the style of the typical UIAlertView popup, I decided to create a custom ConfirmationViewController that I would trigger to popup. Here is what the…
Thomas
  • 2,356
  • 7
  • 23
  • 59
7
votes
3 answers

How to tell what queue a completionHandler executes on?

Looking at the documentation for URLSession.dataTask it's clear this function is called asynchronously but there's no mention of whether the completionHandler returns to the main thread, the thread which called it or remains on the thread the…
7
votes
1 answer

SetValue Completion Block with Firebase 3.x

In Firebase 2.5.1, this was working: let post1Ref = sendRequestRef.childByAutoId() post1Ref.setValue(request, withCompletionBlock: {( error:NSError?, ref:Firebase!) in }) However, I couldn't figure out how to achieve it in 3.x (as the docs for…
senty
  • 12,385
  • 28
  • 130
  • 260
7
votes
1 answer

partial apply for thunk Crash.

Having trouble understanding a crash report in Crashlytics. This is the crash log: Crashed: com.apple.main-thread 0 Rekindlr 0x10007a728 ViewController.(user_info(Match?, completionHandler : (Bool?) -> ()) -> ()).(closure #1)…
Jonovono
  • 1,979
  • 7
  • 30
  • 53
7
votes
3 answers

Returning method object from inside block

I am wondering how to do the following correctly: I have a method that is to return an NSData object. It gets the NSData object from a UIDocument. The NSData object can get large, so I want to make sure it is fully loaded before the response starts.…
Joseph
  • 9,171
  • 8
  • 41
  • 67
6
votes
3 answers

Replicating Swift completion handler on Android & Java

After years, I'm trying to develop an Android app, using Firebase Firestore. I'm basically trying to replicate this Swift function: func getCategories(onCompletion completionBlock: @escaping (_ categories: [Category]?, _ error: Error?) -> Void) { …
6
votes
1 answer

Is it possible to use completion handler in delegate method - Swift

I am trying to handle "google sign in" in singleton helper class. I have LoginHelper, and a method which handles logins with completion handler. As you know Google Sign have delegate methods. When delegate methods are called I need to notify my…
erdemgc
  • 1,701
  • 3
  • 23
  • 43
6
votes
0 answers

How to do login using MVVM?

I am still quite new to iOS development and I am trying to teach myself good coding practices and design patterns in Swift starting with MVVM. I need to pass the data from a completion handle in my ServiceCall class to my ViewModel. I would like…
Erent
  • 571
  • 6
  • 15
  • 29
6
votes
2 answers

FBSDKRequestConnection warning swift3

I've tried to troubleshoot this warning but have had no success. Since upgrading to swift3, I am receiving a warning message in my Facebook Graph Request completion handler. The error message is specifically, "Expression of type…
DevKyle
  • 1,091
  • 6
  • 22
6
votes
2 answers

Fetch data from Firebase by joining tables in iOS

I am trying to fetch data from two different Firebase tables. Here is the structure of table: Post { 1{ pImages{ i1:true i2:true } } 2{ pImages{ i3:true } } } Images{ …
triandicAnt
  • 1,328
  • 2
  • 15
  • 40
6
votes
1 answer

Resuming tasks using NSURLSession when app removed from background or on device reboot

I have checked many docs but could not found solution for Resuming tasks using NSURLSession when app removed from background or on device reboot. I am dealing with amazon S3 to upload some files, In that I am able to Upload file to S3 using…
1
2
3
41 42