Flow is sort of a reactive stream ( like rxjava ). There are a bunch of different operators like .map, buffer() ( anyway less no. Of operator compared to rxJava ). So, one of the main difference between LiveData and Flow is that u can subscribe the map computation / transformation in some other thread using
flowOn(Dispatcher....).
So, for eg :-
flowOf("A","B","C").map { compute(it) }.flowOn(Dispatchers.IO).collect {...} // U can change the execution thread of the computation ( by default its in the same dispatcher as collect )
With LiveData and map , the above can’t be achieved directly !
So its recommended to keep flow in the repository level , and make the livedata a bridge between the UI and the repository !
The main difference is that
- Generally a regular
flowis not lifecycle aware butliveDatais lifecyle aware. ( we can use stateFlow in conjunction withrepeatOnLifecycleto make it lifecycle aware ) flowhas got a bunch of different operators whichlivedatadoesn’t have !
But again , Its up to u how do u wanna construct your project !