Kotlin Flow vs LiveData

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 flow is not lifecycle aware but liveData is lifecyle aware. ( we can use stateFlow in conjunction with repeatOnLifecycle to make it lifecycle aware )
  • flow has got a bunch of different operators which livedata doesn’t have !

But again , Its up to u how do u wanna construct your project !

Leave a Comment

tech