Open ypid opened 10 years ago
@ypid, I suspect that using Thread.sleep
on UI thread will freeze the UI. Personally I would absolutely avoid doing that in the app. It is ok in the tests - because, well, they are just tests.
@evgenyneu Thanks for the info. What if you would put all that in a service? Could you maybe add an example to the test program? I guess the need for a blocking API is kind of common (it would be very helpful at least for me who is trying to write a wrapper class for a JavaScript library)?
@ypid definitelly, synchronous execution can be a very common use case. I will investigate what's the best approach and will put an example in README. Thanks for the feedback.
Thanks very much … I am looking forward to your investigation.
Simply using a CountDownLatch
would work, or you can create a Future
to make it modular...
Somthing like this using a CountDownLatch
:
final CountDownLatch latch = new CountDownLatch(1);
final MutableObject<String> result = new MutableObject<>();
handler.post(new Runnable() {
@Override
public void run() {
webView.evaluateJavascript(js, new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
result.setValue(value);
latch.countDown();
}
});
}
});
latch.await();
result.getValue();
That's the idea, swap out the webView
part and put your asynchronus callback in there; The MutableObject
is just a simple Object wrapper from commons lang, it's trivial to implement yourself if you don't want the added weight of an extra library
@tom91136 Nice nice. Thanks. I will try your suggestions.
Thanks @tom91136, appreciate your help.
thanks a lot @tom91136
Hi
do you know a good way to turn the event driven programming into non-event driven. Lets say you want to define a function like the following example:
I have read some code in your test app:
Is this the way to go?