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

Canceling UseCase job when ViewModel cleared #30

Open
ghost opened this issue Jun 5, 2018 · 7 comments
Open

Canceling UseCase job when ViewModel cleared #30

ghost opened this issue Jun 5, 2018 · 7 comments

Comments

@ghost
Copy link

ghost commented Jun 5, 2018

Do we need to cancel coroutine job in UseCase when ViewModel destroyed (onCleared())?
As was done in previous implementation with Rx observables, which was disposed in Presenter.stop()

@Zhuinden
Copy link

Zhuinden commented Jun 5, 2018

LiveData makes its observers inactive in onStop, so that they resubscribe in onStart.

@Martindgadr
Copy link
Contributor

@anegin, it is part of lifecycle in Activities, Fragments and services so it's not necessary destroy it.

@AppGrade-D
Copy link

Yes, you are right, LiveData won't send any event when receivers (Fragment, Activity) is not ready (is after onStop() state). But in my opinion it not a good practise to leave working thread alive when destroying Activity or Fragment.

Correct me if I'm wrong but when we start suspend function in worker thread in UseCase and does not handle any reference to pending work, we can not cancell it. The only communication from worker thread is via onResult callback.

One of possibilities is to handle Job reference in UseCase class, Job is returned by:

launch(UI) { onResult(job.await()) }

@Zhuinden
Copy link

Zhuinden commented Jul 5, 2018

leave working thread alive when destroying Activity or Fragment.

That depends entirely on what your worker thread is doing, although if it is super-important then you might want to consider a foreground service while the task is running.

@AppGrade-D
Copy link

foreground service while the task is running

In my opinion Foreground Service should be used only for tasks which results is important in context of entire application, but ViewModel is tied to one specific View. But we are getting sidetrack, the problem is: Should we allow to worker thread running when view is completly destroyed?

@Joe-Tang
Copy link

@AppGrade-D You are right.
Should actively cancel the worker thread when view is completly destroyed?

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

No branches or pull requests

6 participants