I am trying to retrive data from server(using retr...
# compose
a
I am trying to retrive data from server(using retrofit). I am retrieving this data on
IO
thread. But as, I use this in below code, It will throw eroor
Not in a frame
Copy code
fun<T> observer(data: LiveData<T>) = effectOf<T?> {
    var result = +state<T?> { data.value }
    val observer = +memo { Observer<T> { result.value = it }}

        +onCommit(data) {
            data.observeForever(observer)
            onDispose { data.removeObserver(observer) }
        }
//        commit()

    result.value

}
I tried to use
open
and
commit
but cannot able to figure out how exactly to use it. When I tried to use
open
i got this error
java.lang.IllegalStateException: no stage finishing is allowed while read is disabled
z
I'm pretty sure you don't need the open and commit calls manually. After retrieving on IO thread, do you switch back to main?
a
How exactly to switch back to main ? In viewmodel I am calling repository(io) code inside
viewModelScope
with main thread
I am doing this
Copy code
private lateinit var nationHeadline: LiveData<ViewState>

init {
    viewModelScope.launch(Dispatchers.Main) {
        nationHeadline = articleRepo.loadHeadlineNation().asLiveData()
    }
}
z
Yeah that's coroutines. Coroutines don't yet work with Compose in my understanding.
In my pet projects I used RxJava behind LiveData and called .observeOn(AndroidScheduilers.mainThread()). It worked without a hiccup. You could give it a try if you're stuck.
a
But in thread @Leland Richardson [G] said something about using
open
https://kotlinlang.slack.com/archives/CJLTWPH7S/p1572188729016400
z
I'm not saying there's no valid usage of open, but your case isn't suffering because of that, but because of coroutines.
a
Ooh, okay
Will .observeOnl work on io thread ? Or I need to call it only in main thread ?
z
you'll probably do something like .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) (you'll need RxAndroid gradle deps for that, it's not part of RxJava)
👍 1
I'm afraid you'll need to read into RxJava a bit
a
Okay
b
You'd also want to not want to use the main dispatcher for a network call. It's recommended to use Dispatchers.IO for that.
i
If you can do your coroutine work on
Dispatchers.Main
for now they do work, just not on any other dispatcher AFAIK
l
fwiw, when coroutines work properly with the new backend, we have plans to make composion imply a coroutine scope, so launching a coroutine from composition will create a scope with a lifetime that matches composition, so things will automatically be cleaned up etc. this also means that the frame you are in will follow the coroutine around so any model objects should “just work” if you’re using coroutines.
💯 2
❤️ 3
z
@Leland Richardson [G] this sounds way too cool to be buried in this thread!
l
i am also excited for it. using coroutines with compose should end up flowing very naturally (pun intended) 😉
i
yeh man that is super awesome can't wait for that! 🙂