Challenge: Suspend Functions | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/10892694-room-database-getting-started/lessons/23

HI filip,
how are you?
In one of my project for the fragment and Activities I had extended the coroutinescope used a job and cancelled the job on onDestroy method.
Now I see the use of lifecyclescope! How is it any different from what I have done earlier and is lifecyclescope more efficient than doing it manually?

Hey there @prasen267!

LifecycleScope is basically the same thing as manual thread and job cancellation handling, except that you don’t have to do any of the work. For the most part, it’s literally the same, as it handles the cancellation of work when the component dies, is main first, and doesn’t bring any overhead.

It may be even better, as you can’t forget to call the cancel functions and the coroutine setup and other small bits you need to set up and clear the scope.

But both of these approaches - lifecycleScope or manual scoping, can be limiting if you need to do some special use cases like persisting calls through some changes i.e. configuration. That’s why you should use ViewModelScope instead, and have them persist through orientation changes.

Hope this helps!