I.e. that a coroutine launched in onStart will get...
# coroutines
p
I.e. that a coroutine launched in onStart will get cancelled in onStop, onCreateView -> onDestroyView, onCreate -> onDestroy; etc?
r
Usually you basically make your Activity/Fragment implement
CoroutineScope by MainScope()
and call
cancel()
in
onDestroy()
p
And how would that cancel onStart jobs in onDestroy?
r
If you started your job with just
launch { }
or something like that in
onStart
, then it’ll die with the rest if I understood it correctly
p
My question was how I can make stuff from onStart not die with the rest, but get cancelled in onStop?
r
Oh, you want something specific to each lifecycle ‘level’? Then you’ll have to do it yourself
p
And how could I do that by myself?
r
You could define multiple scopes with nested jobs I suppose, but I’m not fluent enough in coroutines
`Job`s can have parent `Job`s, I think you’ll need to play with that and make a tree of jobs
I usually just don’t care about things surviving between
onStop
and
onDestroy
... You just have to be sure that there’s nothing left after
onDestroy
. Are you sure the gains are worth the complexity of what you want to do?
p
Sure I am
I.e. I clear my views in onDestroyView, so I don't want a coroutine I started in onCreateView access null views after onDestroyView
r
I usually make things nullable and use calls like
view?.something
so that it’s automagic
p
Nah, that hides bugs
r
I agree that it’s not perfect but I don’t have the time for the complex task you’re talking about. But I guess you could implement all that in subclasses of
Activity
and
Fragment
and then use that
p
I want my code to be correct and not be constantly checking for conditions I didn't write the code to run in
r
I’m still getting lost between scopes, jobs, context, dispatchers from time to time so I don’t feel I’m ready to implement such thing properly. It would take some time and not be guaranteed to work any better. Once I understand everything more I guess I’ll do something like that. Or once everything explodes 😛
Thinking about it, it seems very complex. For example my Presenter just have a reference to the Fragment/Activity, which is a CoroutineScope, and that’s how my Presenter launches coroutines. But in your case you’ll have to pass 3 or 4 scopes to the Presenter, or maybe make them accessible in the Fragment/Activity, and chose carefully which one to use every time...
p
Job is just an interface. I can just create a base class and keep a map of <LifecycleEvent, Job> and just return the correct job depending on the lifecycle.
Wouldn't that work?
r
Hmmm maybe yeah
Make sure you look at how Job cancellation works, I think you need `SupervisorJob`s
Children of a supervisor job can fail independently of each other.
If you use basic `Job`s any error would cancel everything
s
I.e. I clear my views in onDestroyView, so I don't want a coroutine I started in onCreateView access null views after onDestroyView
@Paul Woitaschek A Fragment is a Lifecycle owner and its lifecycle is per default tied to the Fragment itself. This my cause problems as you described above. Instead of this:
Copy code
viewModel.liveData.observe(this, Observer { ... } )
}
Do this
Copy code
viewModel.liveData.observe(viewLifecycleOwner, Observer { ... } )
}
where
this
is replaced by
this.viewLifecycleOwner
This makes sure the LiveData observer will not get callbacks on dead Fragments and only when Views have been created properly, ie between onViewCreated and onViewDestroyed.
p
I don't use LiveData
s
You then could tie the
viewLifecycleOwner
to your own life-cycle observers, which could guard against encountering null values for your views in your Fragments.
p
I'm not talking about any observers, I'm talking about CoroutineScope
s
You could tie your CoroutineScope to this lifecycle-owner, making your CoroutineScope a life-cycle observer.
d
I was looking for some kind of "activity observer" the other day to help solve problems like this
It seems like you can't listen for those events given an instance of
Activity
sadly
someone please correct me because I hope I'm wrong
s
d
oh Awesome!
j
You could create multiple
coroutineScope
properties (not implement them) that gets cancelled on different lifecycle event, then every
.launch
will use correct version of
coroutineScope
. But don’t rely on
job.cancel()
, it will cancel parent job and children, but your “computation” code will still continue to run (null view access might be done after cancelled). So
parentJob.isActive
check should be done. @Paul Woitaschek
s
If you use
LiveData
in your ViewModel, your Activity's or Fragment's observers should never get notified when View references are null. It's true, Jobs are cancelled only at suspension points, not just anywhere in the code. Either rely on those points or query the
isActive
property and bail out.