kotlin-coroutines-flow

How to replace LiveData with Flow

懵懂的女人 提交于 2021-02-08 09:15:36
问题 I've one LiveData named sortOrder and then I've another variable named myData that observes any change to sortOrder and populates data accordingly. class TestViewModel @ViewModelInject constructor() : ViewModel() { private val sortOrder = MutableLiveData<String>() val myData = sortOrder.map { Timber.d("Sort order changed to $it") "Sort order is $it" } init { sortOrder.value = "year" } } Observing in Activity class TestActivity : AppCompatActivity() { private val viewModel: TestViewModel by

How to replace LiveData with Flow

↘锁芯ラ 提交于 2021-02-08 09:13:31
问题 I've one LiveData named sortOrder and then I've another variable named myData that observes any change to sortOrder and populates data accordingly. class TestViewModel @ViewModelInject constructor() : ViewModel() { private val sortOrder = MutableLiveData<String>() val myData = sortOrder.map { Timber.d("Sort order changed to $it") "Sort order is $it" } init { sortOrder.value = "year" } } Observing in Activity class TestActivity : AppCompatActivity() { private val viewModel: TestViewModel by

Kotlin MutableStateFlow.collect is dropping values

♀尐吖头ヾ 提交于 2021-01-05 07:08:51
问题 I have an android app in which I'm trying to use coroutine flows to replace the existing Otto EventBus using my own event bus library. I'm seeing dropped values when setting the MutableStateFlow's value and then collecting in my app's code. I've created a much simpler project that demonstrates the same issue. Any thoughts on why values are being dropped from the MutableStateFlow? Project's build.gradle buildscript { ext.kotlin_version = "1.4.10" repositories { google() jcenter() }

Kotlin MutableStateFlow.collect is dropping values

我怕爱的太早我们不能终老 提交于 2021-01-05 07:07:29
问题 I have an android app in which I'm trying to use coroutine flows to replace the existing Otto EventBus using my own event bus library. I'm seeing dropped values when setting the MutableStateFlow's value and then collecting in my app's code. I've created a much simpler project that demonstrates the same issue. Any thoughts on why values are being dropped from the MutableStateFlow? Project's build.gradle buildscript { ext.kotlin_version = "1.4.10" repositories { google() jcenter() }

How to emit data from an asycnhronous callback using Kotlin Flow?

天大地大妈咪最大 提交于 2020-12-12 06:00:50
问题 I'm starting to learn Kotlin Flow and Coroutines but I do not know how to make the code below works. What am I doing wrong? interface MessagesListener { fun onNewMessageReceived(message: String) } fun messages(): Flow<String> = flow { val messagesListener = object : MessagesListener { override fun onNewMessageReceived(message: String) { // The line below generates the error 'Suspension functions can be called only within coroutine body' emit(message) } } val messagesPublisher =

How to emit data from an asycnhronous callback using Kotlin Flow?

我的梦境 提交于 2020-12-12 05:56:28
问题 I'm starting to learn Kotlin Flow and Coroutines but I do not know how to make the code below works. What am I doing wrong? interface MessagesListener { fun onNewMessageReceived(message: String) } fun messages(): Flow<String> = flow { val messagesListener = object : MessagesListener { override fun onNewMessageReceived(message: String) { // The line below generates the error 'Suspension functions can be called only within coroutine body' emit(message) } } val messagesPublisher =

How to emit data from an asycnhronous callback using Kotlin Flow?

筅森魡賤 提交于 2020-12-12 05:55:26
问题 I'm starting to learn Kotlin Flow and Coroutines but I do not know how to make the code below works. What am I doing wrong? interface MessagesListener { fun onNewMessageReceived(message: String) } fun messages(): Flow<String> = flow { val messagesListener = object : MessagesListener { override fun onNewMessageReceived(message: String) { // The line below generates the error 'Suspension functions can be called only within coroutine body' emit(message) } } val messagesPublisher =