1

Asynctask is no longer available on Android I was running asynctask at the same time as below How do I change the code below to rxandroid?

for(int i=0;i<30;i++)
{
    new AppTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,dbCable, data);
}
swiftbegineer
  • 329
  • 1
  • 9
  • You can still use AsyncTask in all Android versions. That it is deprecated does not mean that it does not work. – blackapps Sep 02 '20 at 07:47
  • RXJava takes a while to learn. Here's a nice, simple replacement for AsyncTask though: https://stackoverflow.com/a/58767934/7434090 – Gavin Wright Sep 02 '20 at 07:52
  • From Android 11, it is said that asynctask cannot be used. If I want to update to Android 11, should I replace asynctask with another one? Thank you for answer – swiftbegineer Sep 02 '20 at 15:04

1 Answers1

2
Disposable subscription = Observable.fromCallable(new Callable() { <your work> })
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidScedulers.mainThread())
    .subscribe({ <success> }, { <error> });

And later you need to call subscription.dispose() somewhere in onStop() or somethink like it in order not to leak something.

And Rx is a cool mechanism for data streams processing. You should consider using Kotlin coroutines if you use it only to do work at background thread. You can learn more about Rx approach here: http://reactivex.io

Anton Potapov
  • 1,265
  • 8
  • 11