In my use case I have a fragment with a data subscription, which fetch data per 3 seconds automatically. And when the fragment is destroyed, this subscription will be stopped because no subscriber using this data subscription.
However, if I opened a popup dialog fragment from this fragment and the dialog fragment is connected with data subscription. I will get a weird user experience that user might wait up to 3 seconds to see the data. I found out that the first fragment is not on paused and it will treat the first call flag as invalid and delay 3 seconds to emit the new data.
case:
Fragment A -> Dialog Fragment B
- FragmentA : onResume
- FragmentB : onResume
Here is my repository class with a temporary solution using simple flag and while loop. I am using flow, how could i send the last result to the new observer so they dun have to wait up to 3 seconds to receive new data?
Appreciate any advice or comments
DataRepository.kt
class DataRepository {
private val isCalledFirstTime = AtomicBoolean(true)
private val latestData by lazy {
flow {
while (true) {
if (hasSubscription()) {
try {
val response = restService.getData()
emit(response)
isCalledFirstTime.set(false)
} catch (e: Exception) {
logger.e(e)
}
}
if (isCalledFirstTime.get()) {
delay(200)
} else {
var count = 0
do {
delay(200)
} while (count++ < (200 / 3000) && !isCalledFirstTime.get())
}
}
}
}
fun observeData() = latestData
}