AndreiBogdan
03/21/2024, 5:19 PMNativeCoroutine
library so I can use StateFlow on iOS. The problem I'm having is it seems it doesn't want to work with generics.
I have the below (focus on uiState
):
open class BaseViewModel<U, R> internal constructor(
uiStateDefault: U,
private val redirectTypeDefault: R
) : KMMViewModel() {
protected val redirectBehaviorSubject = BehaviorSubject(Redirect(redirectTypeDefault))
protected val uiStateMutable = MutableStateFlow(viewModelScope, uiStateDefault)
@NativeCoroutinesState
val uiState: StateFlow<U> =uiStateMutable.asStateFlow()
val redirectObservable: ObservableWrapper<Redirect<R>> =
redirectBehaviorSubject.wrap()
// ....
}
and while on Android it WORKS, on iOS it doesn't. It does see the uiState
but it's seen as Any?
and iOS needs to cast it to whatever U
is in order for it to work.
Is it a limitation of the NativeCoroutine
library; am I using it wrong ?!; is it a limitation of KMM itself ?! I would like not to define uiState
on each viewModel subclass so iOS can see it properly ... does anyone have an idea how to solve this and keep it generic ? :(Rick Clephas
03/21/2024, 5:28 PMNativeCoroutinesState
and just need the actual value you can do the following manually:
@HiddenFromObjC
val uiState: StateFlow<U> =uiStateMutable.asStateFlow()
@ObjCName("uiState")
val uiStateValue: U get() = uiState.value
Rick Clephas
03/21/2024, 5:29 PMAndreiBogdan
03/21/2024, 5:30 PMAndreiBogdan
03/21/2024, 5:30 PMRick Clephas
03/21/2024, 5:31 PMRick Clephas
03/21/2024, 5:31 PMMutableStateFlow(viewModelScope, uiStateDefault)
AndreiBogdan
03/21/2024, 5:31 PMRick Clephas
03/21/2024, 5:31 PMAndreiBogdan
03/21/2024, 5:31 PMAndreiBogdan
03/21/2024, 5:32 PMAndreiBogdan
03/21/2024, 5:32 PMRick Clephas
03/21/2024, 5:33 PMAndreiBogdan
03/21/2024, 5:34 PMAndreiBogdan
03/21/2024, 6:07 PMuiState?.someField
... Not sure if there's anything I can do about that though 🤔Rick Clephas
03/21/2024, 6:27 PMBaseViewModel<U: Any, R>
Michael Krussel
03/21/2024, 7:14 PMAndreiBogdan
03/27/2024, 8:42 AM