I've got a huge problem, hope to find someone helpful.
I am writing my Flutter app and using native sdk. One of them is providing me http client (native in swift) and some methods which are fetching data. One of these methods is a void method with escaping void closure. When i call it i can check if fetch was successful or it failed. If it was successfull i would like to return to Flutter bool true, or if it failed i would like to return false.
Unfortunately i can not return any value directly from closure, because it's void. So i was trying to move my boolean result out from the closure, set it correct value inside and after call return it to flutter. But it's not working, return is being called before closure get's executed. Example below:
private func myNativeMethod(call: FlutterMethodCall) throws -> Bool? {
var result:Bool
client.fetchData(completion: { (res) in
switch res {
case .success():
result = true
case .failure(let error):
result = false
}
})
return result
}
After some research i was trying to use DispatchSemaphore, DispatchGroup, but calling semaphore.wait() is freezing my app completely, dispatchGroup.wait() also does the same. dispatchGroup.notify() is also void so there is no possibility to put return statement inside.
moving semaphore.wait() to another thread is not preventing return from being called.
Does anyone have any idea how to deal with that.
Thanks in advance.
Unfortunately, that's not so simple, you need to use Platform Channels. You can find more info here: https://flutter.dev/docs/development/platform-integration/platform-channels