I am calling a native SDK which goes off and does some API calls, which is obviously time-bound work. For 90% of this work I am using callbacks and method channel calls going bi-directional so Dart can call Swift, and Swift can then call Dart when finished.
The problem I have is when I want to synchronously await the API response (or timeout).
I ran some tests, as I originally was going to just hold off sending back the FlutterResult
until the Swift callback was hit, but this severely slows down the UI, as I believe all platform calls are made on the UI thread. I simulated a 10 second response time and the app would completely hang for a good few seconds before becoming responsive again, and if I spammed a few of these requests the app would hang for 10 seconds plus.
I am a bit confused on what the best way to run synchronous platform code is, because I don't think platform code can be called from an isolate.
I want to
- Get user details
- User presses button
- Show animation whilst waiting
- Get response and move on.
The SHOW ANIMATION WHILE WAITING is the part that of course, janks. I'm not entirely sure why either because running general CPU work whether its native or dart shouldn't freeze the rendering engine surely?
Anyway, the only thing I can think of is:
- Dart calls platform method A
- Set flag
AwaitingMethodA
to true - Immediately return FlutterResult but set a callback in Swift
while (AwaitingMethodA)....
- When callback in Swift hits, jump on the UI thread and send message back to Dart saying complete, which sets AwaitingMethodA to false
- Dart code continues....
^ and I could set a timeout timer for the above to break out the loop if necessary.
Is there anything wrong with this approach, or is there a better convention for synchronous, time-bound platform work?