Jetpack Compose and Room DB: Performance overhead of auto-saving user input?

Miles

I'm writing an app with Jetpack Compose that lets the user input text in some TextFields and check a few radio buttons.

This data is then stored in a Room database.

Currently, I have a "save" button at the bottom of the screen, with a "Leave without saving?" popup.

However, I'd like to get rid of the save button entirely and have it autosave data as it's being typed.

Would the repeated DB queries from typing cause any performance issues? And are there any established best practices for this sort of thing?

Philip Dukhov

With kotlin flow you can use debounce, which is designed specifically for such cases. That way, as long as the user enters text, saveToDatabase will not be called, and when he does not enter a character for some time (in my example it is one second) - the flow will be emitted.

Also during Compose Navigation the view model may be destroyed (and the coroutine will be cancelled) if the screen is closed, in that case I also save the data inside onCleared to make sure that nothing is missing.

class ScreenViewModel: ViewModel() {
    private val _text = MutableStateFlow("")
    val text: StateFlow<String> = _text

    init {
        viewModelScope.launch {
            @OptIn(FlowPreview::class)
            _text.debounce(1000)
                .collect(::saveToDatabase)
        }
    }

    fun updateText(text: String) {
        _text.value = text
    }

    override fun onCleared() {
        super.onCleared()
        saveToDatabase(_text.value)
    }
    
    private fun saveToDatabase(text: String) {
        
    }
}

@Composable
fun ScreenView(
    viewModel: ScreenViewModel = viewModel()
) {
    val text by viewModel.text.collectAsState()
    TextField(value = text, onValueChange = viewModel::updateText)
}

@OptIn(FlowPreview::class) means that the API may be changed in the future. If you don't want to use it now, see the replacement here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is there a library for auto-saving user input (continously)?

Jetpack Compose and Android Room compatibility

JetPack Compose + Room + LiveData + ViewModel

Jetpack compose breaks Room compiler

Jetpack Compose Numeric Input

Issue with jetpack compose animation performance

Auto mirroring in jetpack compose Icons

Room data is not updating inside onDispose Jetpack Compose

Room DataBase Implementation not found w/Jetpack Compose

User online indicator in Jetpack Compose

Build Software Keyboard with Jetpack Compose - IME Input Method with Jetpack Compose

Jetpack Compose mutableStateList vs mutableList Performance

How optimize JetPack Compose list performance?

Jetpack Compose X.dp performance issue?

Auto Height Jetpack Compose Coil Image

Is there Auto refresh in Jetpack Compose viewer in android studio?

Does Android Jetpack Compose support auto animation?

Show trailing icon after text input from user in TextField Jetpack Compose

Android Jetpack Compose: Listen to user's keyboard input not working with Enter key

UseState not saving user input

Saving plot with a user input

Jetpack Compose: Modify Room data class using TextField

How to refresh data from Room when using Jetpack Compose?

Returning a value from a Room database using Jetpack Compose

Room Insert Not Persisting between Runs in Kotlin Jetpack Compose Project

Why is Android app with Jetpack Compose, Room and Dagger-Hilt crashing?

Jetpack Compose align input text in TextField

What is the performance overhead of using a Python ORM to query a DB of objects?

Is it possible to use Paging (As part of android jetpack) without using Room DB?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive