2

Can someone tell me what I am doing wrong

private fun getTorrentsSocketIO() {
    coroutineScope.launch {
        try {
            val socket = IO.socket("...")
            socket.on("torrents") {
                val jsonString = it.toList() as List<Torrent>
                _torrents.value = jsonString
            }
            socket.connect()
        } catch (t: Throwable) {
            Log.i("LIST_VIEW_MODEL", t.message.toString())
        }
    }
}

Error is thrown when the following is called

_torrents.value = jsonString
Gregor A
  • 217
  • 5
  • 16

1 Answers1

14

You should use the postValue.

SetValue will not work for background processes.

Info

This method(setValue) must be called from the main thread. If you need set a value from a background thread, you can use postValue(Object)

Arda Kazancı
  • 8,341
  • 4
  • 28
  • 50