Skip to content

Make transaction updates suspending#592

Merged
MiSikora merged 3 commits into
developfrom
threading-improvement
Mar 29, 2021
Merged

Make transaction updates suspending#592
MiSikora merged 3 commits into
developfrom
threading-improvement

Conversation

@MiSikora

@MiSikora MiSikora commented Mar 27, 2021

Copy link
Copy Markdown
Contributor

📄 Context

There is an issue – #487. While at first I was reluctant to fix it, because I didn't want to do some strange executor delegation inside closing to avoid talking to the database on the main thread, I actually checked the code today and all of the work can be delegated to Room.

📝 Changes

I made transactionUpdate() function suspending. This delegates internally in Room to Architecture Components executor. Additionally I fixed coroutine scopes. Previously we created a scope per request in collector or retention manager. Now they have their own scopes.

🚫 Breaking

No.

🛠️ How to test

Change ReadBytesCallback in the sample to something like this.

class ReadBytesCallback(
    private val byteCount: Long? = null,
) : Callback {
    override fun onFailure(call: Call, e: IOException) {
        e.printStackTrace()
    }

    override fun onResponse(call: Call, response: Response) {
        Handler(Looper.getMainLooper()).post {
            response.body?.source()?.use {
                try {
                    if (byteCount == null) {
                        it.readByteString()
                    } else {
                        it.readByteString(byteCount)
                    }
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
    }
}

It will crash with the original code. Now it behaves correctly.

⏱️ Next steps

Closes #487.

@MiSikora MiSikora requested review from cortinico and vbuberen March 27, 2021 06:12
@MiSikora MiSikora added this to the 4.0.0 milestone Mar 27, 2021
@MiSikora MiSikora merged commit 97cb844 into develop Mar 29, 2021
@MiSikora MiSikora deleted the threading-improvement branch March 29, 2021 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot access database on the main thread

2 participants