Android-CleanArchitecture-Kotlin icon indicating copy to clipboard operation
Android-CleanArchitecture-Kotlin copied to clipboard

Returning LiveData from UseCases

Open svc985 opened this issue 5 years ago • 3 comments

Hello, This project was such an immense Clean Architecture learning tool for me. I'd like to ask what would be a way of returning LiveData from a use case, for example, returning LiveData<List<Movie>> from GetMovies use case and displaying it in MoviesFragment?

Thanks.

svc985 avatar Mar 22 '20 16:03 svc985

I tend to return io.reactivex.Single from "use-case-like things", but I also instead observe the local data store for io.reactivex.Observable directly.

Not sure why you'd want to return LiveData from a usecase.

Zhuinden avatar Mar 23 '20 00:03 Zhuinden

LiveData is associated with the lifecycle of the activity/fragment and generally used between viewmodel - activity/fragment communication. To observe something in usecase, we could make use of coroutine's channels and flows. In the viewmodel, we could use Live data builder to wrap the flow into live-data https://proandroiddev.com/kotlin-flow-on-android-quick-guide-76667e872166

Varsha-29 avatar Jul 12 '20 17:07 Varsha-29

UseCases shouldn't even be stateful. 🤔 That's why LiveData.onActive is powerful, although it'd allow you to lose results if you try to emit multiple events rather than just success/error.

But you don't need LiveData for a success/error.

Zhuinden avatar Jul 13 '20 13:07 Zhuinden