Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to auto cancel UseCase job when the ViewModel is destroyed #118

Open
rommansabbir opened this issue Nov 8, 2021 · 4 comments
Open

Comments

@rommansabbir
Copy link

I want to auto cancel all UseCase request that is tied to the ViewModel.

For now: I have reigstered a callback for ViewModel.onCleared() event. So when the onCleared() is called, the callback will be invoked.

class BaseViewModel : ViewModel(){
    internal var onClearedListener : () -> Unit ={}
    override fun onCleared() {
        super.onCleared()
        onClearedListener.invoke()
    }
}

For UseCase: I have defined two more functions, cancelJob() and monitorViewModelLifecycle(vm: BaseViewModel) and added another overloading function invoke(vm: BaseViewModel,params: Params,onResult: (Either<Failure, Type>) -> Unit = {}).

abstract class UseCase<out Type, in Params> where Type : Any {
    operator fun invoke(
        vm: BaseViewModel,
        params: Params,
        onResult: (Either<Failure, Type>) -> Unit = {}
    ) {
        monitorViewModelLifecycle(vm)
        uiScope.launch { onResult(withContext(Dispatchers.IO) { run(params) }) }
    }

    //Cancel the current ongoing job
    fun cancelJob(message: String = "Abort") {
        uiScope.cancel(message)
    }

    /**
     * Monitor the [BaseViewModel] lifecycle.
     * The motto is to auto clear any ongoing job that is tied to the [BaseViewModel].
     *
     * @param vm, [BaseViewModel] instance
     */
    fun monitorViewModelLifecycle(vm: BaseViewModel) {
        vm.onClearedListener = {
            cancelJob()
        }
    }
}

So, When the ViewModel.onClearedListener is invoked I'm cancelling the current Job.

Is there any better to way to acheive this?

@crjacinro
Copy link

I feel that this code is redundant. You can make use of the viewModelScope as the ui scope for the job. It will get cancelled automatically when the view model is onCleared().

@rommansabbir
Copy link
Author

Yes, we can make use of viewModelScope but stil in some cases I might not execute UseCase from ViewModel, I might execute it from Activity, So I wanted the Coroutine Scope & Job should be under the UseCase from where I can control them instead of passing the Coroutine Scope via params to UseCase.

@crjacinro
Copy link

If you are accessing the use case from an Activity then, you are not adhering to Clean-Architecture principles. Activities should not have access to use cases but only depend on events/data from a ViewModel

@rommansabbir
Copy link
Author

Yes, I completely agree with you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants