Should functions (e.g. in
ViewModel or even
Service) select the
context (withContext(context)
or async(context) {}
) and return
Deferred
, or should
callers have the responsibility to select the context? I would think its cleaner to encapsulate the context choice to a lower level function itself.
Fragment/Activity
calls ViewModel
calls Service
calls API. The Service returns a
Result<T>
, and I originally made ViewModel functions return
Deferred<T>
, so Fragments need to
await
it. However, I found lots of examples calling
async {}
everywhere the functions are called, so callers need to know which context to use, which they will likely know less than the API designer. Also, the
withContext
coroutine function seems to imply people should be calling it in fragments/ highest level. I assume this because
withContext
seems ‘terminal’ (it suspends until it completes).
I am still working on my coroutine understanding, so I would appreciate any help 🙂