Variable outside DispatchQueue.main.async is empty - swift

I tried to resolve this error since days but I dont understand why am I getting this error in the first place.
Please help...
func createData(request:Crudpb_CreateRequest) -> String {
DispatchQueue.main.async {
self.response = try! self.client.create(request)
print("This is response 1: " + self.response.result) // <-------- This is priting the right response
}
print("This is response outside DispatchQueue: " + self.response.result) // <------- This is not printing anyvalue
return self.response.result // <------ This is not
}

You are dispatching whatever work you are doing in your create request method asynchronously, therefor your create data function will not wait for this work to be done to continue its execution, it just calls it and keeps its execution and thats why your value is not modified when you reach your "This is response outside DispatchQueue: " statement.
It will be modified in the capture block that you have created, thats why you need to create an #escaping completion block like they mentioned before, to only return your value when the work you did to obtain it is finished.
func createData(request:Crudpb_CreateRequest, with completion: #escaping (String) -> Void) {
DispatchQueue.main.async {
self.response = try! self.client.create(request)
completion(self.response)
}
}

This is the way I use #escaping closures: You don't need a return value in the function, given that the execution of the calling function may end before the closure is finished. You also need to specify a dataType in the #escaping parameter (Bool in this case)... also, Function types cannot have argument labels, so you must use "_"
self.fetchStuff(onCompletion: { (success) in
if success {
// Do something
}
})
func fetchStuff(onCompletion: #escaping (_ success: Bool) -> Void) {
// Do some asynch stuff
onCompletion(true)
}

Related

Does a Completion Handler end the function?

Perhaps I do not understand the concept of a completion handler, but I have a function like so;
func someFunction(completion: #escaping (Bool, LoginError?) -> Void) {
self.checkDevice() { allowed, error in
if let e = error {
completion(false, e)
}
completion(true, nil)
}
}
While being light on what checkDevice() does, the basic premise is that it performs an asynchronous network call, and returns either true with no error (nil), or returns false with an error.
When I run this code, I am finding that the completion handler is being called twice. It sends a completion as a tuple (as false, error) and also as (true, nil). I've done some debugging and there seems to be no manner in which someFunction() is called twice.
I was of the belief that once a completion is sent, the function would end. In my test case, I am forcing an error from checkDevice(), which should result in me sending the completion as (false, error), but I am seeing both (false, error) and (true, nil). Does a completion not immediately end the function?
I was of the belief that once a completion is sent, the function would end.
No, why would that be? When you call this function it’s like calling any other function. The name has no magic power.
Rewrite it like this:
func someFunction(completion: #escaping (Bool, LoginError?) -> Void) {
self.checkDevice() { allowed, error in
if let e = error {
completion(false, e)
return // *
}
completion(true, nil)
}
}
Actually, I should mention that this is a poor way to write your completion handler. Instead of taking two parameters, a Bool and an Optional LoginError to be used only if the Bool is false, it should take one parameter — a Result, which carries both whether we succeeded or failed and, if we failed, what the error was:
func someFunction(completion: #escaping (Result<Void, Error>) -> Void) {
self.checkDevice() { allowed, error in
completion( Result {
if let e = error { throw e }
})
}
}
As you can see, using a Result as your parameter allows you to respond much more elegantly to what happened.
Indeed, checkDevice itself could pass a Result into its completion handler, and then things would be even more elegant (and simpler).
A completion handler does not end a function. The typical way to end a function is to insert a return.
See comment re: your specific case
Consider including parameter names (for reference). And the completion should only be called once. You can do that using return or by using a full if-else conditional.
func someFunction(completion: #escaping (_ done: Bool, _ error: LoginError?) -> Void) {
checkDevice() { (allowed, error) in
if let e = error {
completion(false, e)
} else {
completion(true, nil)
}
}
}
func someFunction(completion: #escaping (_ done: Bool, _ error: LoginError?) -> Void) {
checkDevice() { (allowed, error) in
if let e = error {
completion(false, e)
return
}
completion(true, nil)
}
}
By giving parameters names, when calling the function, you can now reference the signature to denote the meanings of its parameters:
someFunction { (done, error) in
if let error = error {
...
} else if done {
...
}
}

Swift Completion Handler Not Called on Second Call to Function

I have a Swift function with a completion handler that I want to be called when a certain network request is done. The network request can be repeated based on if the server response says there is more data (offset is provided).
I initially call it like this:
func myMainFunction(){
getTasks(project: project, paginationToken: nil) {
print("This never fires...")
}
}
And my getTasks() function is like this:
func getTasks(project: Project, paginationToken: String?, completion: #escaping () -> Void){
var url = "..."
if let token = paginationToken{
url += "&offset=\(token)"
}
Alamofire.request(url).responseJSON { response in
switch response.result {
case .success(let value):
let json = JSON(value)
//...
//Check for pagination
let offset = json["next_page"]["offset"].stringValue
if !offset.isEmpty{
//Theres a pagination token available, go again!
getTasks(project: project, paginationToken: offset){}
}else{
print("This prints to the log on the last run through")
completion()
}
case .failure(let error):
print("Task error: \(error)")
}
}
}
If there is never an offset then the completion() handler works fine. But if getTasks() has to be called again, the completion handler is somehow invalidated and never returns to my original myMainFunction().
Any ideas?
You are passing empty closure if offset is not empty. You need to forward the completion param to getTasks again. Try this.
//...
if !offset.isEmpty {
getTasks(project: project, paginationToken: offset, completion: completion)
}
//...

Chaining closures and completion handlers Swift 3

I'm having a hard time understanding how chaining completion handlers with closures works.
The example I'm working with is the following :
typealias CompletionHandler = (_ result: AnyObject?, _ error: NSError?) -> Void
func getRequestToken(completionHandler: CompletionHandler){
taskForGET(path: "PathToWebsite.com") { (result, error) in
if let error = error {
print(error)
} else {
print(result)
}
}
}
func taskForGET(path: String, completionHandler: CompletionHandler) {
//URLSESSIONCODE with completion handler
(data, response, error) {
if let error = error {
CompletionHandler(result: nil, error: error)
} else {
let data = data
parseJSON(data: data, completionHandler: completionHandler)
}
}
}
func parseJSON(data: NSData, completionHandler: CompletionHandler) {
//SerializingJSON with do catch block
do {
completionHandler(result: theResult, error: nil)
} catch {
completionHandler(result: nil, error: error)
}
}
So basically what this code does is it kicks off a GET request to a server. If the GET request sends back data, then it parses it into JSON. If at any point along the way something fails, it returns an error.
I understand basically what is going on here, but I don't understand how the completion handlers are being fired off.
First taskForGET is called which has a completion handler as a parameter that can return a result or an error, I've got that.
The next step is calling parseJSON, where the data from taskForGET is passed but then the completionhandler that's being passed is taskForGET's completion handler. I don't understand this at all.
Then down in parseJSON, the completion handler either returns JSON or an error by calling the completion handler from its parameters..which in this case is taskForGET's completion handler?
I don't understand the flow. Even once we've parsed JSON successfully, how does calling taskForGET's result ever get back up to getRequestToken.
Any help with this would be appreciated.
There is only one completion handler which is passed from one method to another.
Lets declare the handler separately, btw. in Swift 3 omit the parameter labels in the type alias:
typealias CompletionHandler = (Any?, Error?) -> Void
let handler : CompletionHandler = { (result, error) in
if let error = error {
print(error)
} else {
print(result)
}
}
This closure is supposed to be executed at the end of the process. Now the getRequestToken method looks like
func getRequestToken(completionHandler: CompletionHandler){
taskForGET(path: "PathToWebsite.com", completionHandler: handler)
}
The handler / closure is passed as a parameter from getRequestToken to taskForGET and then from taskForGET to parseJSON but it's always the same object.

Result of call is unused

Right below the second comment, I receive an error of "Result of call to 'taskForDeleteMethod' is unused. Why is this when I use the results and error in the closure following the call?
func deleteSession(_ completionHandlerForDeleteSession: #escaping (_ success: Bool, _ error: NSError?) -> Void) {
/* 1. Specify parameters, method (if has {key}), and HTTP body (if POST) */
// There are none...
/* 2. Make the request */
taskForDELETEMethod { (results, error) in
/* 3. Send the desired value(s) to completion handler */
if let error = error {
print("Post error: \(error)")
completionHandlerForDeleteSession(false, error)
} else {
guard let session = results![JSONKeys.session] as? [String: AnyObject] else {
print("No key '\(JSONKeys.session)' in \(results)")
return
}
if let id = session[JSONKeys.id] as? String {
print("logout id: \(id)")
completionHandlerForDeleteSession(true, nil)
}
}
}
}
In earlier swift versions, you need not bother about the return value of a method. You may store it in any variable snd use it later or you may ignore it completely. Neither it gave any error nor a warning.
But in swift 3.0 you need to specify whether you want to ignore the returned value or use it.
1. If you want to use the returned value, you can create a variable/constant and store the value in it, i.e
let value = taskForDELETEMethod {
// Your code goes here
}
2. If you want to ignore the returned value, you can use _ ,i.e
let _ = taskForDELETEMethod {
// Your code goes here
}
You are confusing the results variable, which is, indeed, used inside the closure, and the result of the taskForDELETEMethod call itself, which is NSURLSessionDataTask object.
From the examples of using taskForDELETEMethod that I was able to find online it looks like it is perfectly OK to ignore the return value, so you can avoid this warning by assigning the result to _ variable, i.e.
let _ = taskForDELETEMethod {
... // The rest of your code goes here
}

func writeImageToFile(path: String, completeBlock: (success: Bool) -> Void){ }

i am trying to upload images using DKimagepickercontroller and this is a way to upload it to a url but i am confuse what i am suppose to put into the completeBlock: (success: Bool) - Void
func writeImageToFile(path: String, completeBlock: (success: Bool) -> Void){
}
this is the code that i wrote in xcode
let apath = "http://localhost/swift/upload.php"
writeImageToFile(apath,completeBlock: (success: false) -> Void)
but i got this error
expected expression in the list of expressions and this error expexted ',' separator
You have to call it like that writeImageToFile(path) { success in print(success) }
This block is supposed to be used as a trailing closure, don't leave it in the signature.
It looks like this in Xcode: stackoverflow.com/a/33020097/2227743
The closure argument is used once the task is done, in your case it will indicate if the file has been written or not.
You can for example test success in the closure:
writeImageToFile(somePath) { (success) in
if success {
// the file has been written
} else {
// the file hasn't been written
}
}