swiftbegineer
swiftbegineer

Reputation: 331

How can I change asynctask code to rxandroid code in android?

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);
}

Upvotes: 1

Views: 69

Answers (1)

Anton Potapov
Anton Potapov

Reputation: 1275

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

Upvotes: 2

Related Questions