Do you have any sample code/project that using coroutines in the android custom view?
I would like to learn the best practice that handle coroutines in custom view.
m
myanmarking
04/09/2021, 12:04 PM
there are no best practices in coroutines in custom views:P coroutines should not live in the viewlevel. If you really have to, i guess you could cancel the scope in onViewDetached, maybe
s
stojan
04/10/2021, 4:00 PM
My strategy is to avoid if possible and keep the view dumb.... network calls live in the VM since they have to survive config changes anyway and the view is only interested in the result
in certain scenarios it might make sense to expose suspend functions from the View and launch the coroutines from the lifecycle owner
if 1 and 2 don't work then I might consider creating a scope and what ☝️ suggested
g
gildor
04/10/2021, 4:04 PM
New version of lifecycle library allows you access lifecycle scope of view' (Activity or Fragment), search for findViewTreeLifecycleOwner
🙌 1
gildor
04/10/2021, 4:07 PM
Though, I agree, that in most cases you shouldn't launch coroutine from view (and extract business logic to VM), but coroutines are very useful on view if you use it to view-only related stuff, like animations
👍 1
gildor
04/12/2021, 6:45 AM
yeah, I don’t see any issues with this use case to use coroutines