Michael Granger
06/07/2022, 8:41 PMVivek Modi
06/09/2022, 3:03 PMMutableStateFlow
in my project. When we initialise the MutableStateFlow
object we need to give default value.
val topics = MutableStateFlow<List<String>>(emptyList())
when I collect this value
[null, "Hello", "world"]
I want to pass this list in adapter
. So is there a way we can remove the null object before passing in adapter or Is there any better way ?
viewModel.topics.collect { topicsList ->
println(topicsList) // [null, "Hello", "world"]
adapter.submitList(topicsList)
}
Vikas Singh
06/09/2022, 7:10 PMColton Idle
06/09/2022, 11:14 PMcurrentUser = Flow<User>
Would this be better represented as a StateFlow?Peter Mandeljc
06/16/2022, 2:58 PMdistinctUntilChanged()
, with the exception, to re-emit same value, after certain amount of time has passed. How would I achieve that?
time [sec], input values, emitted value
0, "A", "A"
1, "A", <no emission>
2, "A", <no emission>
3, "A", "A"
4, "B", "B"
Aidan Low
06/21/2022, 5:29 PMstateFlow1
and stateFlow2
via .stateIn(started = SharingStarted.WhileSubscribed)
and then create stateFlowCombined = stateFlow1.combine(stateFlow2).startIn(started = SharingStarted.WhileSubscribed)
then will subscribing to stateFlowCombined
transitively subscribe to stateFlow1
and stateFlow2
? I'm struggling with writing unit tests to verify behavior that works fine in our production app.AmrJyniat
06/24/2022, 12:19 PMvar property: String? = null
.......
propertyFlow.collect{
property = it
}
.......
textView.text = property // reflect this value when its get ready
Something like that, any idea?Vikas Singh
06/27/2022, 5:59 AMfun initiate(){
viewModelScope.launch {
discoverTag = usecase.tag
}
}
Where tag
is stateflow in usecase which emits the value
The problem here is that the discover tag is only collected for the first time but not when emitted from usecaseColton Idle
07/28/2022, 3:36 AMAmrJyniat
07/28/2022, 9:07 AMFlow
each x seconds? like:
val flow = flow {
emit(doApiCall())
}
.onEach{ delay(1000) }// something like that
........
flow.collect{
//get flow value
}
I searched a lot but it seems that I need to put the code in fun and call it each time, what do you think?Robert Wijas
07/28/2022, 4:14 PMfun myFlow(): Flow<B> =
valueFlow()
.map { B(it) } // B is the type with expiration date
.flatMapLatest { value ->
flow {
emit(value)
value.timeToExpiration?.let {
delay(it)
emitAll(myFlow()) // !recursion
}
}
}
Any hints how to remove recursion? Thx.Landry Norris
07/28/2022, 4:29 PMLandry Norris
08/05/2022, 3:02 PMalthaf
08/21/2022, 4:28 AMlifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
loginAuthorizationViewModel.onLoginSuccessful.collect {
startActivity(HomeActivity.getDefaultIntent(this@LoginActivity))
finish()
}
}
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
loginInputValidationViewModel.onPasswordInvalid.collect {
enableViews(true)
et_password.error = it
}
}
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
loginAuthorizationViewModel.onLoginFailure.collect {
enableViews(true)
showMessage(it.toString())
}
}
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
loginInputValidationViewModel.onUserNameInvalid.collect {
enableViews(true)
tv_user_name.error = it
}
}
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
loginInputValidationViewModel.onValidationSuccessful.collect {
loginAuthorizationViewModel.requestLogin(
userName = it.first,
password = it.second
)
}
}
Natalia Chuzhmarova
09/07/2022, 7:49 AMfun data(): Flow = flowOf(
flowOf(cachedValue),
networkCallFlow
). flattenConcat()
which is then observed in a ViewModel and mapped to UI state
data()
.onEach { .. }
.map { uiState(data) }
.catch { emit(uiState(error)) }
It works fine when the network call succeeds. The problem is, when networkCallFlow
throws an exception (no network), the cachedValue
is never mapped, however it triggers the onEach
handler. My guess is that the emission of the cachedValue
does not guarantee that it will be observed, and the exception halts the Flow before the consumption of the cachedValue
happens or while it is in progress.
Is my understanding correct?
Is there any way to adjust the Flow to fit the requirements?
The code: https://pl.kotl.in/3U8jVprL-jozefdransfield
09/12/2022, 2:39 PMalthaf
09/17/2022, 7:11 AMjava.lang.IllegalArgumentException: Flow context cannot contain job in it. Had [SupervisorJobImpl{Active}@7b4ca1f, <http://Dispatchers.IO|Dispatchers.IO>]
interface UseCaseScope : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = SupervisorJob() + <http://Dispatchers.IO|Dispatchers.IO>
}
The intention here is to cancel the coroutine context when we business logic need to cancel it. So that i can call coroutineContext.cancel()
class LoginUseCase(private val authRepository: AuthRepository) : FlowUseCase<LoginRequest, Boolean>() {
override fun execute(parameter: LoginRequest): Flow<Result<Boolean>> =
flow {
authRepository.login(parameter.username, parameter.password).collect { response ->
this.defaultResultHandler(onSuccess = {
emit(Result.success(true))
}, response)
}
}.flowOn(coroutineContext)
override fun cancel() {
coroutineContext.cancel()
}
}
^^^ will cause app the crash
Or is this is the way to execute my intention, and ignore flowOn(.....) , i'm really confused here
class LoginUseCase(private val authRepository: AuthRepository) :
FlowUseCase<LoginRequest, Boolean>() {
override fun execute(parameter: LoginRequest): Flow<Result<Boolean>> =
flow {
>>>> launch(coroutineContext) {
authRepository.login(parameter.username, parameter.password).collect { response ->
this@flow.defaultResultHandler(onSuccess = {
emit(Result.success(true))
}, response)
}
}
}
override fun cancel() {
>>> coroutineContext.cancel()
}
}
Suraj Sahani
09/28/2022, 3:52 AMSam Stone
10/12/2022, 2:17 AMZach
10/14/2022, 7:25 PMstateFlow
that combines two snapshotFlow
that works when running on device, but when I attempt to unit test with turbine, it never emits more than once. Am I doing something wrong or am I going down the wrong path?
fun validInput() = runTest {
viewModel.screenState.test {
assertEquals(awaitItem(), EnterNameScreenState.EnterNameLoadingResults) // passes
viewModel.firstNameInputValidation("test")
// fails, waited too long
assertEquals(awaitItem(), EnterNameScreenState.EnterNameLoadedResults(InputWrapper(value = "test"), InputWrapper(), false)).
}
}
AmrJyniat
10/19/2022, 8:44 AMretry
mechanism?Victor Collod
10/27/2022, 3:30 PMVictor Collod
10/27/2022, 4:07 PMelye
10/31/2022, 6:48 AMMarc Knaup
11/03/2022, 8:03 PM.onEach { … }.launchIn(…)
) elements can be missed if they’re emitted shortly after this.Rihards
11/11/2022, 1:02 PMStateFlow
-> when I open the screen for the first time and do actions to emit new values then value is updated, but when I re-open the view then value is not updated to initial value, but old value is still there.
This is the way I try to keep the state up to date:
private val valueChanges: MutableSharedFlow<Int> = MutableSharedFlow(
extraBufferCapacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST
)
private val valueState: StateFlow<Int> = valueChanges
.stateIn(scope, SharingStarted.Eagerly, 0)
Then at the very beginning of the whole flow I call this function where changes
is valueChanges
flow:
updateChangesOnStart(changes: Flow<Int>) = changes
.onStart { emit(0) }
To update the state later I use:
scope.launch{
valueChanges.emit(${passed number})
}
Is the scope the one that does not allow me to update the value when function is reused?Racci
11/16/2022, 7:35 AMliminal
12/27/2022, 3:49 AMliminal
12/27/2022, 4:26 PM