LukasLechnerDev / Kotlin-Coroutines-and-Flow-UseCases-on-Android

🎓 Learning Kotlin Coroutines and Flows for Android by example. 🚀 Sample implementations for real-world Android use cases. 🛠 Unit tests included!
Apache License 2.0
2.63k stars 433 forks source link

Add info about Workmanager gotcha #4

Closed CarsonRedeye closed 4 years ago

CarsonRedeye commented 4 years ago

Here's something that caught me out while using WorkManager with coroutines that you might want to add: Do not call a suspend function in the catch block (when you are trying to set the action as failed in the database etc). It will not be called when the work is cancelled due to system constraints. In that case WorkManager will throw JobCancellationException which means suspend functions will re-throw immediately. If you really want the suspend function to run, you must use NonCancellable context. See: https://kotlinlang.org/docs/reference/coroutines/cancellation-and-timeouts.html#run-non-cancellable-block

LukasLechnerDev commented 4 years ago

Hi! Thank you for this info! As you seem to have a good understanding of this gotcha, I would really appreciate it if you could submit a PR with some comments in the workmanager use case so that people don't make the same mistake as you.

CarsonRedeye commented 4 years ago

Sure will do