0

After migrating to androidx, my room repositories based on LiveData and GlobalScope.launch stopped working.

I changed GlobalScope.launch to GlobalScope.async, because i got exception with observeForever (but I event don't use this method).

        val success = MediatorLiveData<SomeDataModel>()
        try {
            GlobalScope.async(Dispatchers.IO) {
                success.addSource(myDao.getAll()) {
                    it?.let {
                        success.postValue(it)
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return success
    }

I want to point that before migrating to androidX this code worked.
pjp
  • 157
  • 1
  • 12

1 Answers1

0

Code inside an async block does not execute until you await it somewhere else. If you're coming from a JavaScript world, this will surprise you because in JavaScript, async code executes before it's ever awaited.

user1713450
  • 1,307
  • 7
  • 18