How to perform time-consuming operations in the background and update the UI using coroutines in Kotlin for Android development in Jetpack Compose - multithreading

I'm developing an android app that is a bitcoin wallet using Jetpack Compose.
I have Wallet.kt file with:
fun sync() {
Log.i(TAG, "Wallet is syncing")
wallet.sync(blockchain, LogProgress)
}
fun getBalance(): ULong = wallet.getBalance().total
then in HomeScreen.kt I have
internal class WalletViewModel() : ViewModel() {
private var _balance: MutableLiveData<ULong> = MutableLiveData(0u)
val balance: LiveData<ULong>
get() = _balance
fun updateBalance() {
Wallet.sync()
_balance.value = Wallet.getBalance()
}
then outside of this is composable function HomeScreen
internal fun HomeScreen(
navController: NavController,
walletViewModel: WalletViewModel = viewModel()
) {
val balance by walletViewModel.balance.observeAsState()
Image(Modifier.clickable{ walletViewModel.updateBalance() }
}
My problem being that when I click on that Image which has clickable, the whole app freezes, until the updateBalance() is completed.
I learned that this is because the sync() function inside Wallet.kt file is performing network task on the Main Thread and the app is in Main Thread, so the whole app has to wait until sync is done.
Can you suggest how should I implement coroutines or different way, so that the sync happens inside background thread and then updates _balance to/in the Main ?
I've tried lots of things, including suspend before sync() and async in the viewModelScope, but nothing seems to work how I want to.
Thanks

You can solve it in two ways, one is to attach the async call into the viewModelScope and mark the async call as suspend, the other one is creating a coroutine in your repository with Context and execute it in another thread.
Solution 1
fun updateBalance() {
viewModelScope.launch {
Wallet.sync()
_balance.value = Wallet.getBalance()
}
}
suspend fun sync() {
Log.i(TAG, "Wallet is syncing")
wallet.sync(blockchain, LogProgress)
}
Solution 2
suspend fun sync() {
Log.i(TAG, "Wallet is syncing")
withContext(Dispatcher.IO) {
wallet.sync(blockchain, LogProgress)
}
}

Related

How to send to Flutter's EventChannel from another thread?

I sucessfully setted a event channel between Kotlin and Flutter. However, I need another Kotlin thread to be able to use this Event Channel. I only found tutorials on how to launch things from the Event Channel inside the onListen method, like making a counter that sends things every second.
How can I make another thread call event.sucess(something)?
Here's what I done:
class MainActivity: FlutterActivity(){
companion object {
//this is called by another thread
#JvmStatic
private fun onEventChannel(b: ByteArray):Int {
//How do I make this call events.success(b) on onListen?
}
//...
override fun configureFlutterEngine(#NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
EventChannel(flutterEngine.dartExecutor.binaryMessenger, EVENT_CHANNEL).setStreamHandler(object : EventChannel.StreamHandler {
override fun onListen(args: Any, events: EventChannel.EventSink) {
Log.d(TAG, "adding listener for $EVENT_CHANNEL")
events.success(true)
}
override fun onCancel(args: Any) {
Log.d(TAG, "cancelling listener for $EVENT_CHANNEL")
}
})

ViewModelScope and IdlingResource

in my project im using AndroidX Lifecycle ViewModel (2.2.0-rc01) and im in the need to write a couple of espresso test, to tell espresso that the Application tested is still busy.
The viewModelScope need to tell that espresso is busy.
val ViewModel.viewModelScope: CoroutineScope
get() {
val scope: CoroutineScope? = this.getTag(JOB_KEY)
if (scope != null) {
return scope
}
return setTagIfAbsent(JOB_KEY,
CloseableCoroutineScope(SupervisorJob() + Dispatchers.Main.immediate))
}
internal class CloseableCoroutineScope(context: CoroutineContext) : Closeable, CoroutineScope
{
override val coroutineContext: CoroutineContext = context
override fun close() {
coroutineContext.cancel()
}
}
Is extending viewModelScope and using the CountingIdleResource a valid solution ?
Or do i just need the InstantTaskExecutorRule ?
The Coroutines Job has #invokeOnCompletion function which i could use to count down the idling resource.
thanks
It looks like there is some ongoing discussion: https://github.com/Kotlin/kotlinx.coroutines/issues/242

JavaFX working with threads and GUI

I have a problem while working with JavaFX and Threads. Basically I have two options: working with Tasks or Platform.runLater. As I understand Platform.runLater should be used for simple/short tasks, and Task for the longer ones. However, I cannot use any of them.
When I call Thread, it has to pop up a captcha dialog in a middle of task. While using Task, it ignores my request to show new dialog... It does not let me to create a new stage.
On the other hand, when I use Platform.runLater, it lets me show a dialog, however, the program's main window freezes until the pop up dialog is showed.
I need any kind of solution for this. If anyone knows how to deal with this or had some similar experience and found a solution I am looking forward to hearing from you!
As puce says, you have to use Task or Service for the things that you need to do in background. And Platform.runLater to do things in the JavaFX Application thread from the background thread.
You have to synchronize them, and one of the ways to do that is using the class CountDownLatch.
Here is an example:
Service<Void> service = new Service<Void>() {
#Override
protected Task<Void> createTask() {
return new Task<Void>() {
#Override
protected Void call() throws Exception {
//Background work
final CountDownLatch latch = new CountDownLatch(1);
Platform.runLater(new Runnable() {
#Override
public void run() {
try{
//FX Stuff done here
}finally{
latch.countDown();
}
}
});
latch.await();
//Keep with the background work
return null;
}
};
}
};
service.start();
Use a Worker (Task, Service) from the JavaFX Application thread if you want to do something in the background.
http://docs.oracle.com/javafx/2/api/javafx/concurrent/package-summary.html
Use Platform.runLater from a background thread if you want to do something on the JavaFX Application thread.
http://docs.oracle.com/javafx/2/api/javafx/application/Platform.html#runLater%28java.lang.Runnable%29
It's too late to answer but for those who have the error, here is the solution XD
You can use one Thread.
Use the lambda expression for the runnable in the thread and the runlater.
Thread t = new Thread(() -> {
//Here write all actions that you want execute on background
Platform.runLater(() -> {
//Here the actions that use the gui where is finished the actions on background.
});
});
t.start();
You can user directly this code
Don't forget you can't send non-final variable in thread .
you can send final variable in thread
//final String me="ddddd";
new Thread(new Runnable() {
#Override
public void run() {
// me = me + "eee";
//...Your code....
}
}).start();
Use in
your code
try/catch

Working with threads in blackberry

I am using threads in blackberry to perform web service calls. I want to get notified as soon as the call gets a response back. I was using
Handlers
in android. I didnt find anything similar in blackberry.
Here is the code I am using to run the thread
class PrimeRun implements Runnable {
long minPrime;
PrimeRun(long minPrime) {
this.minPrime = minPrime;
}
public void run() {
// compute primes larger than minPrime
. . .
}
}
How can I get a notification after the thread finished running?
How can I do this in blackberry?
Thanks
Added more Information : Thanks for your reply. Its really
informative. Let me explain a bit more on my issue. I have a
webservice call which is running on a thread. As soon as I get the
reply back from server I want to execute the next function(next call
to server) which is based on the response from the previous call.So I need to wait until I get a response back. Also
at them same time I need to show a activity indicator on screen. I was
using handler for this in android. I am looking for something similar
on blackberry.
So your question essentially is this
One thread does the job while the other thread waits for completion
The first thread completes the job and "notifies" the second thread.
This is a simple producer consumer problem. Here is the code how you can solve this.
class JobResult
{
boolean done = false;
}
JobResult result = new JobResult();
class Worker extends Thread
{
JobResult _result;
public Worker( JobResult result )
{
_result = result
}
public void run()
{
// Do some very long job
synchronized( _result )
{
// modify result
_result.done = true;
_result.notify();
}
}
}
public class Waiter extends Thread
{
JobResult _result;
public Waiter( JobResult result )
{
_result = result;
}
public void run()
{
synchroinzed( _result ){
while(! _result.done)
{
this.wait();
}
}
// Wait is over. You can do something now.
}
}
As I got the Zach's question - he asks how to execute some code that involves UI changes (something like showing an info popup or closing the progress popup) upon a background thread completion. On Android a Handler created on the UI thread is often used for that purpose.
In BB you can use another way which is similar to Swing on desktop Java. When you need some code to be executed on the UI thread you wrap it in a Runnable and pass to one of the following methods:
// Puts runnable object into this application's event queue,
// and waits until it is processed.
Application.invokeAndWait(Runnable runnable)
// Puts runnable object into this application's event queue.
Application.invokeLater(Runnable runnable)
// Puts runnable object into this application's event queue
// for repeated execution.
Application.invokeLater(Runnable runnable, long time, boolean repeat)
So the behaviour of the above calls is similar to what Handler.post(Runnable r) (and the like) does.
Note, you can always get a handle to your Application instance by a static call Application.getApplication().
So in the end of a background thread it is safe to do something like this:
Application.getApplication().invokeLater(new Runnable() {
public void run() {
progressScreen.close();
Dialog.alert("I am finished!");
}
});
It is similar to Android's:
handler.post(new Runnable() {
public void run() {
progressScreen.dismiss();
showDialog(DIALOG_TASK_FINISHED_ID);
}
});
Android has a much rich multi threading primitives. But you can achieve the same even in Blackberry with equal elegance. The solution I provide below is essentially the same as previous, but with a minor change. Waiter thread can be replaced with built-in utility to perform painting on UI thread using UiApplicaiton's invokeLater method. You don't actually need to "notify" anyone but just update the UI once a particular task is completed. Check the docs for more info.
Anyway, you can model your code along the lines:
class ProgressScreen extends FullScreen
{
LabelField _label;
public void start()
{
}
public void setMessage( final String message )
{
UiApplication.getApplication(
UiApplication.invokeLater(
new Runnable() {
_label.setText( message );
}
)
);
}
public void dismiss()
{
this.close();
}
}
interface WebserviceTask
{
int STATUS_CONDITIONS_NOT_SATISFIED = -3;
int STATUS_NET_ERR = -2;
int STATUS_FAILURE = -1;
int STATUS_SUCCESS = 0;
public int invoke();
}
public class Updater extends Thread
{
final int NUM_TASKS = 10;
WebServiceTask tasks[] = new WebServiceTask[ NUM_TASKS ];
WebServiceTask tasks[0] = new WebServiceTask(){
public int invoke()
{
int retCode = 0;
// invoke a particular web service
return STATUS_SUCCESS;
}
}
public void run()
{
ProgressScreen progress = new ProgressScreen();
progress.start();
for( int i=0; i < NUM_TASKS; i++ )
{
int retcode;
WebServiceTask t = tasks[i];
retcode = t.invoke();
String mesg;
switch( retcode )
{
case STATUS_SUCCESS: { mesg ="Task successfully completed!";} break;
case STATUS_NET_ERR: { mesg ="Could not connect to network";} break;
}
progress.setMessage(message);
}
progress.dismiss();
}
}
Note that I have provided only the stubs to give you an idea how you may accomplish. Let us know how it goes.

Is there a prefered approach for introducing a delay before a WCF call

As my user changes the CurrentItem of a dataForm, I need to go the server to get addtional data. It's quite likely that the user could scroll through several items before finding the desired one. I would like to sleep for 500ms before going to get the data.
Is there a component already in the SDK or toolkit like a background worker that would assist in getting back to the UI thread to make my WCF async call once the 500ms sleep is done? It seems that if I don't do that, and try instead to call the WCF async method on the sleeper thread then the Completed event fires on the sleeper thread and not the UI thread, which of course is not good.
I think you might be a little off-track in your thinking. I'm not sure why you feel you need to get back to the UI thread in order to make the asych call. Generally you do as much work as you can on a BG thread and only marshal back to the UI thread when you have the results (by way of the Dispatcher).
I typically use a System.Threading.Timer for this purpose:
public class MyViewModel
{
private readonly Timer refreshTimer;
public MyViewModel()
{
this.refreshTimer = new Timer(this.DoRefresh);
}
public object CurrentItem
{
get { ... }
set
{
...
Invalidate();
}
}
// anything that should invalidate the data should wind up calling this, such as when the user selects a different item
private void Invalidate()
{
// 1 second delay
this.refreshTimer.Change(1000, Timeout.Infinite);
}
private void DoRefresh()
{
// make the async call here, with a callback of DoRefreshComplete
}
private void DoRefreshComplete()
{
// update the UI here by way of the Dispatcher
}
}

Resources