https://kotlinlang.org logo
#coroutines
Title
# coroutines
f

Florian

12/01/2020, 6:58 AM
Is
invokeOnCompletion
a good place to hide a progress bar if a Coroutine was either finished or cancelled?
Copy code
.invokeOnCompletion {
    _loading.postValue(false)
}
👍 1
a

andylamax

12/01/2020, 6:59 AM
absolutely
hey, are you the youtuber codeinflow?
f

Florian

12/01/2020, 8:58 AM
yes 👍
thank you
a

andylamax

12/01/2020, 9:38 AM
good content you creating. Good stuff
f

Florian

12/01/2020, 9:38 AM
Thank you very much 🙏
t

travis

12/01/2020, 8:09 PM
Here is a very long winded comment I had on a PR on the topic (of using
invokeOnCompletion
): https://gist.github.com/twyatt/c61ebc97dd891ff7a888311df857f619 Hopefully it's helpful.
f

Florian

12/01/2020, 9:36 PM
Thank you. Do you think it's a good idea to use it for my progress bar LiveData?
t

travis

12/02/2020, 6:16 PM
For your progress bar example, it's a simple enough case that
invokeOnCompletion
should work just fine. My personal preference is
try-finally
as it has more capabilities if needed. Allows for more consistency in codebases for cases that need the capabilities of
try
-
finally
approach (rather than having some
invokeOnCompletion
and some
try
-
finally
, just have all be
try
-
finally
). But that's just a preference.
f

Florian

12/02/2020, 9:11 PM
hm but I have the try/catch in the repository layer
and the progress bar LiveData in the ViewModel
a

andylamax

12/02/2020, 9:13 PM
if you have try catch on the repository, how do you present your errors to the UI? I suggest move your try catch to your viemodel, that way incase of any error, you can still handle it and display to the user what went wrong
f

Florian

12/02/2020, 9:13 PM
well the catch block returns a Result.Error
a

andylamax

12/02/2020, 9:15 PM
If you are already handling errors in the repo side, then
invokeOnCompletion
would be the best way to go for your progress bar
2
f

Florian

12/03/2020, 9:14 AM
Thank you 👍
6 Views