-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
liveData with Coroutines #715
Comments
Hi @isfaaghyth . Your liveData from repository.getData() don`t have active observers. So liveData not emiting data. Try something this. ...
fun fetch():LiveData<Data> {
return repository.getData(viewModelScope)
}
}
... class Repository @Inject constructor(val service: Service) {
fun getData(scope:CoroutineScope): LiveData<Data> = liveData(scope.coroutineContext) {
val data = service.getData()
emit(data)
}
} class SomeFragment:Fragmet(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { ...
viewModel.fetch().observe(viewLifecycleOwner, Observer{
println(it)
}
}
} P.S. |
so, which a good way for my case? @alexkazancew |
@isfaaghyth i update my previous answer |
it works! thanks @alexkazancew |
How about using |
@GuilhE I think you can do something like this
Let me know if it helped you. |
Hi! who already use for this one
androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha01
? I've tried it into my repository layer but doesn't work (i'm usingretrofit 2.6.0
). this is my code:and this is my viewmodel:
the result is
null
.any idea about this?
thanks!
The text was updated successfully, but these errors were encountered: