Is it OK to launch coroutines from Globalscope on Android in certain situations (singletons)?

后端 未结 2 1973
旧时难觅i
旧时难觅i 2021-02-20 07:10

When launching coroutines from Activities, Fragments or Android Architecture Components ViewModels, it makes total sense to use a coroutine scope that is bound to the lifecycle

2条回答
  •  我在风中等你
    2021-02-20 07:20

    Given that you're already trying to attach it to application's lifecycle, I'd suggest either passing the scope to your singleton or implementing a coroutinescope by it. Unfortunately, running coroutines on GlobalScope still might end in leaks. See this great article by Roman Elizarov for more info: https://medium.com/@elizarov/the-reason-to-avoid-globalscope-835337445abc

提交回复
热议问题