1. Are you ready for the Galaxy S20? Here is everything we know so far!

Cannot invoke observer on a background thread

Discussion in 'Android Development' started by Paolo Giovanelli, Oct 29, 2021.

  1. Paolo Giovanelli

    Thread Starter

    hi all, I'm implementing a REST Full WebService API which has only one GET method.
    Inside this method I created an observer to read the <LiveData> returned by a function, but when I try to read it gives me an error: cannot invoke observe on a background thread
    Here my code:

    @AndroidEntryPoint
    class MainActivity : AppCompatActivity()
    {
    private lateinit var verViewModel: VerificationViewModel
    private lateinit var qrCodeText: String
    var certStatus: String = "vuoto"


    override fun onCreate(savedInstanceState: Bundle?)
    {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)


    verViewModel = ViewModelProvider(this)[VerificationViewModel::class.java]

    embeddedServer(Netty, 8000)
    {
    install(StatusPages) {
    exception<Throwable> { e ->
    call.respondText(e.localizedMessage, ContentType.Text.Plain, HttpStatusCode.InternalServerError)
    }
    }


    install(ContentNegotiation)
    {
    gson {}
    }


    routing{
    get("/getStatus/{qrText}") {
    certStatus ="vuoto"
    qrCodeText = CastNullableString(call.parameters["qrText"])
    certStatus = validateGreenPass(qrCodeText)
    call.respond(Response(status = certStatus))
    }
    }
    }.start(wait=true)
    }


    data class Response (val status: String)

    fun CastNullableString(str:String?):String
    {
    if (str == null)
    {
    return ""
    }
    else
    {
    return str
    }
    }


    private fun validateGreenPass(qrCodeText: String):String
    {
    var status:String = "Vuoto"
    try
    {
    val certObserver = Observer<CertificateSimple?> {
    fun onChanged(@nullable personModel: SimplePersonModel,@nullable date:String,certificateStatus:CertificateStatus,@nullable time: Date){
    status = certificateStatus.toString()
    }
    }
    verViewModel.certificate.observe(this,certObserver)
    (when execute this give me error)
    verViewModel.decode(qrCodeText, false);
    }
    catch (e: Exception)
    {
    status = "ERROR: " + CastNullableString(e.message)
    }
    return (status)
    }
    }

    Anyone know how to help me?
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Cannot invoke observer
  1. brentc
    Replies:
    3
    Views:
    429
  2. wizard7611
    Replies:
    5
    Views:
    758
  3. meer baloch
    Replies:
    0
    Views:
    642
  4. CiDefaid
    Replies:
    3
    Views:
    1,643
  5. Bob65
    Replies:
    1
    Views:
    1,004
  6. Janeire
    Replies:
    2
    Views:
    384
  7. mikewax
    Replies:
    0
    Views:
    658
  8. unet
    Replies:
    0
    Views:
    857
  9. DridriLaBastos
    Replies:
    0
    Views:
    774
  10. kgash
    Replies:
    6
    Views:
    901

Share This Page

Loading...