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

iOS, Push, didReceiveRemoteNotification, impact of UIBackgroundFetchResult

I understand what "UIBackgroundFetchResult" means. But what is the impact? Does something behave different when setting the completionHandler to "UIBackgroundFetchResultNewData" than "UIBackgroundFetchResultNoData"? Is that relevant for Apples Push…
0
votes
0 answers

Update UIProgressView from FBRequest CompletionHandler

I'am going crazy about this. I Know I have to update the UI from the main thread and tried both the performSelectorOnMainThread method and wrapping the progressView update in dispatch_async( dispatch_get_main_queue(), ^{ update progressView…
0
votes
2 answers

textFieldShouldReturn - some methods in completionHandler are never called

I have a UITextField which lets the user enter a custom Album name for Facebook Photos. Once textFieldShouldReturn, I take the textField.text and send it to Facebook. Once the completion handler is called, I would like to update my model and reload…
n.evermind
  • 11,944
  • 19
  • 78
  • 122
0
votes
1 answer

sendAsynchronousRequest and completionHandler in return method

How can I return data when completionHandler finishes? I tried this method, but it shows an error: - (NSData *)ReturnDataFromUrl{ NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]]; …
0
votes
1 answer

ForkJoinTask completion handler

I have a long-running calculation that I have split up with Java's ForkJoinTask. Java's FutureTask provides a template method done(). Overriding this method allows for "registering a completion handler". Is it possible to register a completion…
Abdull
  • 26,371
  • 26
  • 130
  • 172
0
votes
3 answers

In iPhone how do I know when all of my map's annotations have loaded while using forward geocode?

In my app I have a table view that toggles the annotations on a map view. I can go back and forth between the table view and map view through the tab bar controller. My map will reload the annotations (from the selected items that are in the table…
mcphersonjr
  • 733
  • 12
  • 35
0
votes
1 answer

Seems like my app is doing stuff in the wrong order

(Note: You can probably even answer this without reading any of my code, if you don't feel like reading this novel of a question, I've explained at the bottom) I have some code that's throwing me a bit off course. It seems like my code is calling…
Sti
  • 8,275
  • 9
  • 62
  • 124
0
votes
1 answer

CLGeocoder delay until completion handler

I have an app that requires Geocoding from a string to lat/long values, I use the geocoder from the iOS class…
Uba Duba
  • 241
  • 2
  • 12
-1
votes
0 answers

Swift function updating variable but not returning it

Good evening, after spending weeks trying to figure this out, I am looking for some guidance. I am trying to get a coordinate object to return and then go into a compact map. The function is working, because when I do a print, I can see it, but…
-1
votes
1 answer

How to ensure Completion handler for one Firebase lookup completes before another one does

I have the following Firebase DB node structure: UserInGroup --- GroupID --- UserId : true/false Users --- UserId --- Username : String --- ... GroupStatus --- GroupId --- UserId: true/false I need to…
Learn2Code
  • 1,974
  • 5
  • 24
  • 46
-1
votes
1 answer

The function returns the result before processing the completionHandler of another function, how to fix it?

I ran into such a problem, and I can't figure out what are the ways to solve this problem. First, let's say I have the following function func summ(x: Int, y: Int, completionHandler: @escaping (Int) -> ()) { let result: Int = x + y …
Resly34
  • 7
  • 4
-1
votes
1 answer

how to pass function in parameter using closure in swift 5

I am new in swift. I have made generic actionsheet import Foundation extension UIAlertController{ func action(mes:String,tit:String,tit2:String,operation1:(),operation2:()) { let actionSheet = UIAlertController(title: "",…
getc
  • 1
  • 5
-1
votes
1 answer

Confusing Declaration with a Swift Completion Handler

I was reading the textbook "iOS 14 Programming for Beginners" by Armad Sahar. There are codes below in a MapDataManager class which I was stuck for days. import Foundation import MapKit class MapDataManager: DataManager { fileprivate var items:…
Bosco Tsin
  • 172
  • 1
  • 11
-1
votes
1 answer

how to return a value from completion handler

I want to put the value I get from the completion handler into Text view as a string, but I get an error. Here is what I try to do: func final(name: String, completion: @escaping (_ message: String)-> Void){ guard let uid =…
erdem
  • 41
  • 2
-1
votes
1 answer

Swift5 Extra argument 'completionHandler' in call error with session.dataTask

I am trying to make a some weather app but there is some problem The error is "Extra argument 'completionHandler' in call" what should i do for solve it ?? enter image description here import Foundation struct WeatherManager { …
Macvin
  • 3
  • 2