0

I have been learning about Kotlin Flows lately. I have a few questions in understanding them. Please help me clarify these things:

  1. If we don’t have to send multiple values(in case we only use REST APIs), in that case what is the advantage of using Kotlin Flows over LiveData?
  2. How is the data sent from a streaming API to the app?
  3. How do we generally integrate streaming APIs in an android app if we use/don’t use Kotlin flows?
Neeraja Gandla
  • 97
  • 1
  • 6
  • 17
  • 1
    For question 1, a suspend function would be superior to a Flow or LiveData because it would be more natural to use. The other questions are quite open ended. – Tenfour04 Nov 17 '21 at 03:41
  • So when we need to use REST API we can simply use a suspend function that returns a List. It serves the purpose in that case, doesn't it? I'm trying to understand why is there a need to use/learn Flows if we don't use streaming APIs. – Neeraja Gandla Nov 17 '21 at 07:58
  • 1
    Flows and LiveData are both not great fits for non-streaming API. They are both for reactive programming, but if you're just retrieving one thing, it's usually more natural to request that thing and react to it in the same place in code, so a suspend function is easier. You could also use them to preload a single thing and get it later in some other code, but in Kotlin a Deferred would make more sense than a Flow for this purpose. In Java-only code, you could use LiveData or CompletableFuture for this. – Tenfour04 Nov 17 '21 at 14:21
  • 1
    A "benefit" of using a Flow approach can be leveraged if you instead use [StateFlow](https://developer.android.com/kotlin/flow/stateflow-and-sharedflow), since they are "smarter" and will waste less resources. In any case, if you're just doing a one shot operation, like it's been mentioned, a `suspend fun` in some interactor/repo while you could observe a LiveData value from the UI through a VModel that delivers a `sealed` class with different states. – Martin Marconcini Nov 17 '21 at 16:27

0 Answers0