Hi i am trying to get data from my database but i ...
# announcements
r
Hi i am trying to get data from my database but i am getting error
Caused by: java.lang.IllegalStateException: Cannot invoke setValue on a background thread
this is my code written on Java (Im new on Android), someone knows why it’s the problem whit my line?
Im using retrofit 🤔
v
do you have a
postValue()
? (i do not know exactly what libraries are you using)
i
doInBackground()
is, as the name implies, on a background thread. Sounds like you should be returning that object and doing the
setValue
in
onPostExecute()
which is what runs on the main thread
👍 2
r
For get the data Im using retrofit \
i
That's kind of the entire reason those are separate methods
r
Im using this libraries but i not have idea how I can solve this error )
c
AsyncTask is now officially deprecated since Android 11, so you should consider converting that class to Kotlin and using Coroutines to manage async Retrofit API calls instead.
But since you’re posting the value to a LiveData, you can use
.postValue()
to set the value from a background thread and it should work (though I’d consider that a code smell)
o
@RX Would you mind telling us how you even got to using an
AsyncTsk
? This thing fell out of favor a good few years ago. Where did you learn about
AsyncTask
? Because that tutorial needs to be taken down.