Is `invokeOnCompletion` a good place to hide a pro...
# coroutines
f
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
absolutely
hey, are you the youtuber codeinflow?
f
yes 👍
thank you
a
good content you creating. Good stuff
f
Thank you very much 🙏
t
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
Thank you. Do you think it's a good idea to use it for my progress bar LiveData?
t
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
hm but I have the try/catch in the repository layer
and the progress bar LiveData in the ViewModel
a
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
well the catch block returns a Result.Error
a
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
Thank you 👍