1

Im trying to fix this from a very long time.

I can not understand what to pass to this function in place of "(String) -> void" as it was supposed to return a string :

var result = myobj.createData(request: request, with: (String) -> void)

The above code is calling the following function:

func createData(request:Crudpb_CreateRequest, with completion: @escaping (String) -> Void) {
    DispatchQueue.main.async {
        self.response = try! self.client.create(request)
        completion(self.response.result)
    }
}
  • This is a closure. You can read more about this [here](https://docs.swift.org/swift-book/LanguageGuide/Closures.html). – TheTiger May 08 '19 at 12:08

1 Answers1

1

When you call this function you need to pass a closure with the type (String) -> void

myobj.createData(request: request, with: { string in
    print(string)
})

Or

var completion = { string in
    print(string)
}
myobj.createData(request: request, with: completion)

You can store the result like this

var result = ""
myobj.createData(request: request, with: { string in
    result = string
    self.displayTextArea.text = result
    print(result)
})
RajeshKumar R
  • 13,989
  • 2
  • 35
  • 63
  • When I try to access the result of the result variable from outside the function it is empty and not updated even when the result variable was created outside the function. Im trying to update my UI with the result but it is not happening because of this. My code looks like this now: var result = "" myobj.createData(request: request, with: { string in result = string print("result in closure: " + result) }) print("result OUTSIDE closure: " + result) displayTextArea.text = result – Karina sarkisovae May 08 '19 at 12:33
  • But I got another problem, when I try to stop the server and test the ios app in simulator it freezes. How to avoid this and how to give the user a friendly message like "Sorry server is not available?" – Karina sarkisovae May 08 '19 at 13:01
  • It says I can only post a question once in 90 minutes. :( – Karina sarkisovae May 08 '19 at 13:10
  • @Karinasarkisovae Check https://stackoverflow.com/questions/1083701/how-to-check-for-an-active-internet-connection-on-ios-or-macos – RajeshKumar R May 08 '19 at 13:10
  • @Karinasarkisovae Have you checked above link? – RajeshKumar R May 08 '19 at 13:13
  • I wasn't talking about testing whether there is an internet connection available or not. I was talking about handling the error if the server doesnt respond or is down. – Karina sarkisovae May 08 '19 at 14:24
  • I posted the question now: https://stackoverflow.com/questions/56042998/how-to-handle-error-in-swift-when-server-is-not-available – Karina sarkisovae May 08 '19 at 14:24
  • But I see that the button which triggers the request remains disabled until the result is shown in the UI. Why is this happening? I thought the reason to use DIspatchQueue.main.async was to keep the UI enabled even while the server is still processing and returning back the response. – Karina sarkisovae May 08 '19 at 14:36
  • I installed the app on my iphone and tested to see whether the UI gets disabled while fetching result from server and I was right the UI is getting disabled until it receives the result. What to do now? Seems like using DispatchQueue.main.async didnt made any difference. :( – Karina sarkisovae May 08 '19 at 17:59
  • @Karinasarkisovae self.response = try! self.client.create(request) DispatchQueue.main.async { completion(self.response.result) } – RajeshKumar R May 08 '19 at 18:13
  • That still didnt didnt work. Do we need another completion block like in this: http://prntscr.com/nm6d36 ?? If yes then how to write it? :O – Karina sarkisovae May 08 '19 at 20:40
  • I attempted to write code and I did something like this: http://prntscr.com/nm6u1s but I think I dont know what Im doing at all, Im completely lost. – Karina sarkisovae May 08 '19 at 21:23
  • Please check my new thread: https://stackoverflow.com/questions/56049420/how-to-not-freeze-the-ui-and-wait-for-response – Karina sarkisovae May 08 '19 at 22:05