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

Backend Completion Handler crashes app if completed after PushViewController

What I have I don't understand the nature of the problem. I have 2 View Controllers : 1) FeedViewController ,which shows events in tableview 2) EventViewController, pushed when you press on event. When Feed Loads, it start asynchronous loadings of…
0
votes
4 answers

Swift: How to call functions where one of the arguments is a completionHandler

I'm very new to XCode and Swift and in my iOS app I've written a method to perform a POST request to a php file on a server by following some answers here in stackoverflow: func closeTaskService(id_task:Int, completionHandler: (response: NSString)…
SagittariusA
  • 5,289
  • 15
  • 73
  • 127
0
votes
1 answer

Swift Array is Empty After Parse Queries - Completion Handler?

I don't understand why the arrays become empty after the query with block. I did some research and it's most likely because I need a completion handler, but I can't figure out how to implement it in this case. Can I just add an activity indicator…
Petruc
  • 23
  • 4
0
votes
0 answers

Code after NSURLSession runs before session is over - Swift

I have an app that accesses a website and downloads the HTML from that site, converts it to a string, grabs some information from the string, and then uses that for the app. My problem is that the code extracting the string runs before the data can…
Jaxon
  • 303
  • 1
  • 2
  • 10
0
votes
3 answers

Swift: Block mainThread until function finished loading data / NSURLSession with completionHandler

I want to create a function which loads data from an url and then returns the responseData as NSData. I want to block the mainThread until the data is finished. Here is what I have so far: Function: typealias CompletionBlock = (NSData!,…
ixany
  • 5,433
  • 9
  • 41
  • 65
0
votes
1 answer

Return value while using closure as a Completion Handler In Swift

func loadMoreData(offset: Int, completion: (result: [ArtistJSONMapper]?) -> Void) { var fetchedData = [ArtistJSONMapper]() let pageNum: Int = offset/paging.limit // Calling the json fetch to obtain data JSONFetch.jsonTest() {…
Prashant Khanal
  • 249
  • 2
  • 11
0
votes
1 answer

Proper way to return data from VC without losing scope

I have solved my problem, but I don't know if there is some better way of doing that.I am making a workout log. From VC1 I want to select a date and push the VC 2 to select an exercise and then return the exercise to VC1. I am getting the returned…
crom87
  • 1,141
  • 9
  • 18
0
votes
3 answers

How to cancel a completion handler in iOS

I want to get an array of nearby locations using mapkit framework. So when the user types in a textfield I call the following function. - (void)searchForLocations:(NSString *)string { [NSObject cancelPreviousPerformRequestsWithTarget:self…
jjpp
  • 1,298
  • 1
  • 15
  • 31
0
votes
1 answer

Using completionhandler from function variable in NSURLConnection's sendAsynchronousRequest issue

I have a function : func connectHTTP(requests: String, completion:(NSURLResponse!, NSData!, NSError!) -> Void) { (...) NSURLConnection.sendAsynchronousRequest(httpRequest, queue: queue, completionHandler: completion) } If I call…
0
votes
1 answer

Return value in delegate method but with completion hander

I have a map with the view controller as it's delegate, I call the below method to add some custom pins to the map and also get the route to them. I want to add the distance of the route to the pins, so I need to calculate the route then add the…
0
votes
1 answer

How to return a value from a function that is generated from a parse query within the function?

Problem: when I call the below function within my ViewController tableView cellForRowAtIndexPath method, I get nil, but I need to set the generated object to a PFObject variable called rankObject that I declared globally in my ViewController. I've…
FaceRake
  • 3
  • 3
0
votes
1 answer

NSURLSession Response String completion block - Swift

I want to wait for a responseString to complete before calling the next function "nextScreen()" (segue). At the moment I have an if statement to make sure it is not nil before proceeding, but sometimes the the next function/segue is called because…
ThundercatChris
  • 481
  • 6
  • 25
0
votes
1 answer

CloudKit fetchUserRecordIDWithCompletionHandler completion code not calling?

I am trying to fetch the current user ID from CloudKit using fetchUserRecordIDWithCompletionHandler but when I run the code, the completion handler is skipped over. let container = CKContainer.defaultContainer() let publicDatabase =…
0
votes
1 answer

reverseGeocode - completionHandling a completionHandler? (Swift)

What is the best way to achieve this? I have a func that gets the reverseGeocode for a given longitude/latitude and it works just fine. But because it is asynchronous by the time it has got the address information I have already executed the lines…
Edward Hasted
  • 3,201
  • 8
  • 30
  • 48
0
votes
2 answers

Return data out of http sendAsyncRequest call in swift

Ok so first of all, I've read several other post on the subject and I know that what Im going to describe could be a synchronous call but what I really need is to get the data and work with it, so if I should look in some other direction let me…
SKYnine
  • 2,708
  • 7
  • 31
  • 41