Hank Moody
asked on
Cant access variables inside the DispatchQueue.main.async
The variable is empty outside the DispatchQueue.main.async and I dont know what to do exactly.
Im trying to make this work from so many days but I gave up now.
Please help...
Im trying to make this work from so many days but I gave up now.
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
}
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Beside this you should make API call in a background thread and return result in main thread.
Something like this should work:
https://medium.com/@oleary.audio/simultaneous-asynchronous-calls-in-swift-9c1f5fd3ea32
A part from link:
Open in new window
Based on above example your code should look like this:
Open in new window