Kotlin LiveData, StateFlow, SharedFlow
Kotlin LiveData, StateFlow, SharedFlow

In Kotlin Language SharedFlow, StateFlow, and LiveData are classes that comes with Kotlin Coroutines library which are basically used for communication between component in android application in an asynchronous way. However, there are many key differences in terms of their functionality & use cases.

Difference between SharedFlow, StateFlow, LiveData

1. Kotlin LiveData

In Kotlin language LiveData is a Data Model Class used to hold data and is designed to continously observed by it’s UI components may be activities and fragments. Kotlin LiveData is designed to hold and keep observing data that can be attached to the lifecycle of android component such as Activity or a Fragment. In Other words LiveData automatically handles updates to the UI Component when there are active observers and stops updates when there are no active observers, this helps us in preventing memory leaks. Note that LiveData is not a component or feature of Kotlin Coroutines library. Code Example Below

2. Kotlin Stateflow

In Kotlin StateFlow is a part asynchronous event stream, non-blocking I/O Stream that updates all the subsequent states and it’s current state of the emited data to i;s observers. As I said StateFlow is also a part of Kotlin coroutines library that gives a way to handle and represent state-based data flows. In Kotline StateFlow is used in ViewModel to immediatly update when the state changes to UI components. You might be wondering StateFlow seems similar to LiveData but it offers more flexibility, specially when it is combined with coroutines, as it support you to get control over data emission and transformation. Code Example Below

3. Kotlin Sharedflow

In Kotlin SharedFlow is another way to handle asynchronous stream, non-blocking updates, but unlike as we saw in StateFlow, When a observer starts observing SharedFlow does not emit it’s current state. SharedFlow is been designed for use cases when the initial state is not critical or you can ignore it. SharedFlow is designed in such a way that it allows multiple collectors to receive the emitted values concurrently. Suppose you have multiple subscribers and you want to broadcast data to all the subscribers we can use SharedFlow in Kotlin. Code Example Below



Kotlin LiveData Example

Here is a simple example how to integrate kotlin LiveData

1. Add LiveData dependencies in your android kotlin `build.gradle` file under dependencies section:

dependencies {
    def lifecycle_version = "2.4.0-alpha03"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
}

2. Create a class of which data you want to observe:

data class User(val name: String, val age: Int)

3. Create ViewModel Class that contains a LiveData Object

class UserViewModel : ViewModel() {
    private val _user = MutableLiveData<User>()
    val user: LiveData<User> get() = _user

    fun updateUser(newUser: User) {
        _user.value = newUser
    }
}

4. Now Finally you can keep observe changes of the ‘user’ LiveData Object in your Activity

class MyActivity : AppCompatActivity() {
    private val viewModel by viewModels<UserViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        viewModel.user.observe(this, { user ->
            // Update the UI with the new user data
        })

        // Call the update function to trigger the observer
        viewModel.updateUser(User("John Doe", 30))
    }
}


Kotlin Stateflow Example

Here is a simple example how to use kotlin StateFlow into your android application:

1. Add StateFlow dependencies in your android kotlin `build.gradle` file under dependencies section:

dependencies {
    def lifecycle_version = "2.4.0-alpha03"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
}

2. Create a class of which data you want to observe:

data class User(val name: String, val age: Int)

3. Create ViewModel Class that contains a StateFlow Object:

class UserViewModel : ViewModel() {
    private val _user = MutableStateFlow(User("", 0))
    val user: StateFlow<User> get() = _user

    fun updateUser(newUser: User) {
        _user.value = newUser
    }
}

4. Now Finally you can keep observe changes of the ‘user’ StateFlow Object in your Activity or in Fragment by using coroutunes:

class MyActivity : AppCompatActivity() {
    private val viewModel by viewModels<UserViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        lifecycleScope.launch {
            viewModel.user.collect { user ->
                // Update the UI with the new user data
            }
        }

        // Call the update function to trigger the observer
        viewModel.updateUser(User("John Doe", 30))
    }
}


Kotlin Sharedflow Example

Here is a simple example How to use kotlin SharedFlow into your android application:

1. Add SharedFlow dependencies in your android kotlin `build.gradle` file under dependencies section:

dependencies {
    def lifecycle_version = "2.4.0-alpha03"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
}

2. Create a class of which data you want to emit:

data class Message(val text: String)

3. Create ViewModel Class that contains a ShareFlow Object:

class MessageViewModel : ViewModel() {
    private val _messages = MutableSharedFlow<Message>()
    val messages: SharedFlow<Message> get() = _messages

    fun sendMessage(message: Message) {
        viewModelScope.launch {
            _messages.emit(message)
        }
    }
}

4. Now Finally you can keep observe and collect the emitted data/message/changes of the ‘user’ StateFlow Object in your Activity or in Fragment by using coroutunes:

class MyActivity : AppCompatActivity() {
    private val viewModel by viewModels<MessageViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        lifecycleScope.launch {
            viewModel.messages.collect { message ->
                // Do something with the message
            }
        }

        // Call the send function to emit a new message
        viewModel.sendMessage(Message("Hello, world!"))
    }
}