Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DB ANRs during form entry #5867

Merged
merged 30 commits into from
Jan 22, 2024
Merged

Fix DB ANRs during form entry #5867

merged 30 commits into from
Jan 22, 2024

Conversation

seadowg
Copy link
Member

@seadowg seadowg commented Dec 7, 2023

Blocked by #5836
Work towards #5838

Fixes DB ANRs caused by DB access on the UI thread for the form entry flow (the bits covered by tests at least).

The main changes here:

  • Removes all write operations against the Forms and Instances DB from the UI thread
  • Removes most of the read operations against the Forms and Instances DB from the UI thread
  • Adds a custom StrictMode call to prevent any writes getting added
  • Adds custom StrictMode calls to repo methods to prevent them being used on the UI thread again

I also ended up introducing Kotlin's Flows as an alternative to LiveData in one particular place (in BlankFormListViewModel). This was because I needed to do a DB lookup in a ViewModel based on the value of a LiveData. This is doable, but ends up being pretty clunky as observations are always run on the UI thread (which keeps LiveData nice and safe), so we'd be jumping back and forwards between threads. Flow lets us choose where transformations/observations are run (similar to RxJava). I've made sure that we can use Flow with Scheduler so that we're not deviating from our existing async setup.

Why is this the best possible solution? Were any other approaches considered?

I'd have preferred to move all the forms/instances DB operations to the background and be able to have the StrictMode calls constrained to just DatabaseConnection, but it started to feel like too much work for one PR - fixing the cases within the form entry flow felt like a good chunk of progress in the end.

How does this change affect users? Describe intentional changes to behavior and behavior that could have accidentally been affected by code changes. In other words, what are the regression risks?

There's definitely a bunch of risk to form entry in general here, but there's nothing specific I'd have in mind to test. Playing around with form entry and seeing if there are ways to break it is all I can suggest really! It's not worth trying to reproduce the ANRs as none of them are going to be easy to recreate reliably.

Before submitting this PR, please make sure you have:

  • added or modified tests for any new or changed behavior
  • run ./gradlew connectedAndroidTest (or ./gradlew testLab) and confirmed all checks still pass
  • added a comment above any new strings describing it for translators
  • verified that any code or assets from external sources are properly credited in comments and/or in the about file.
  • verified that any new UI elements use theme colors. UI Components Style guidelines

override fun onChanged(o: T) {
data = o
latch.countDown()
this@getOrAwaitValue.removeObserver(this)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This didn't work with cases where there was already a value set on the LiveData.

Copy link
Member

@grzesiek2010 grzesiek2010 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two new comments.

@seadowg
Copy link
Member Author

seadowg commented Jan 22, 2024

Looks like that last commit has broken an instrumentation test, so will mark this as a draft for the moment while I fix.

@seadowg seadowg marked this pull request as draft January 22, 2024 16:27
@seadowg seadowg removed the request for review from grzesiek2010 January 22, 2024 16:27
@seadowg seadowg marked this pull request as ready for review January 22, 2024 17:19
@grzesiek2010 grzesiek2010 merged commit a57d49b into getodk:master Jan 22, 2024
6 checks passed
@seadowg seadowg deleted the db-anr branch January 23, 2024 08:39
@srujner
Copy link

srujner commented Jan 26, 2024

Tested with Success!

We didn't found any strange behaviors or issues related to the Form Entry in General

@dbemke
Copy link

dbemke commented Jan 26, 2024

Tested with Success!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants