TeamAmaze / AmazeFileManager

Material design file manager for Android
https://teamamaze.xyz
GNU General Public License v3.0
5.24k stars 1.56k forks source link

Convert all AsyncTasks to Callables called from ReactiveX #2668

Open EmmanuelMess opened 3 years ago

EmmanuelMess commented 3 years ago

public class ReadFileTask extends AsyncTask<Void, Void, ReturnedValueOnReadFile> { to public class ReadFileTask implements Callable<ReturnedValueOnReadFile> {

and used as

final ReadFileTask task = new ReadFileTask(getContentResolver(), viewModel.getFile(), getExternalCacheDir(), isRootExplorer());

final Consumer<ReturnedValueOnReadFile> onAsyncTaskFinished = (data) -> {
//operations
}

final Consumer<? super Throwable> onError = error -> {
  error.printStackTrace();
}
Flowable.fromCallable(task)
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(onAsyncTaskFinished, onError);
VishalNehra commented 3 years ago

We seem to have a better solution now https://developer.android.com/topic/libraries/architecture/coroutines

So we use viewmodel like we normally would, and then something like viewModelScope.launch{} to launch a computation in that viewmodel. It'll be bound to it, so when viewmodel clears, the coroutine would be cancelled as well. We won't have to maintain it's state.

EmmanuelMess commented 3 years ago

We seem to have a better solution now developer.android.com/topic/libraries/architecture/coroutines

So we use viewmodel like we normally would, and then something like viewModelScope.launch{} to launch a computation in that viewmodel. It'll be bound to it, so when viewmodel clears, the coroutine would be cancelled as well. We won't have to maintain it's state.

I thought this as a step towards that.

EmmanuelMess commented 3 years ago

We seem to have a better solution now developer.android.com/topic/libraries/architecture/coroutines

So we use viewmodel like we normally would, and then something like viewModelScope.launch{} to launch a computation in that viewmodel. It'll be bound to it, so when viewmodel clears, the coroutine would be cancelled as well. We won't have to maintain it's state.

How does this deal with Context leaks?