Dean Djermanović
07/24/2021, 8:45 AMUI , Presentation , Business and Data . I’m launching a coroutines from the Presentation layer where I have setup backgroundScope with an exception handler. I want to share a Flow in the data layer using shareIn operator. Which scope should I pass to it? If I pass different scope than the backgroundScope used in the presentation layer, the exceptions won’t be propagated up to the exception handler in that backgroundScope . Also I want to share that Flow globally, not in the scope of that ViewModel in the presentation layer. So the question is how to share a Flow on the data layer from some repository class which is a singleton and still propagate the exceptions to the exception handler that’s setup in the presentation layer?Nick Allen
07/25/2021, 6:42 PMFlow to be a singleton? This becomes trivial if you just share within the Presentation layer.
If you want backgroundScope to handle the exception, you have to throw it in the Presentation layer. If the Data layer encounters an error, it can encode that as an error event in the Flow (part of a sealed class or Result). The Presentation layer can re-throw the exception when it encounters the error event. This pattern of encoding errors into the data is what the SharedFlow docs mean when they say "All errors and completion signals should be explicitly materialized if needed"
Don't forget about situations if an error may happen when there is no Presentation layer (do you need to save it?), or if many collectors may encounter the same Data layer exception (will you spam the user or crash reporting?).