简体   繁体   中英

How to improve Room query times

I'm using Android Room and combination of LiveData in my app successfully, but I notice it has a significant delay between query and presenting the data to the UI. It is not the end of the world, not unsustainable at all, but when opening other messaging Apps (like WhatsApp) the difference is monumental. When I open such an app the conversations and messages are already available, there isn't even time to play the fade in default animation that is typically available in RecyclerView lists.

On the other hand in my app I can clearly see the delay the data takes until it is presented to the UI.

I understand that attaching the viewmodel to a fragment means that same viewmodel is reconstructed every time the associated fragment is rebuilt, so in theory attaching that viewmodel to the activity's would mean that data would survive even longer in memory, thus saving loading times when rebuilding the fragment, but the very first time it loads it still will show that significant delay, so this isn't an ideal solution nor do I know if this would be an ill advised modification (attach the fragment's viewmodel to the activity lifecycle instead of fragment's).

Are there any ways that I can improve this? Is perhaps WhatsApp using a completely different SQLite library that is marginally faster than Room so I will never be able to achieve this performance with Room?

I think, you have to investigate the issue before try to fix, possible bottle necks:

  1. slow database queries reasons:
  • huge amount of data
  • not optimized queries
  • middleware converts (if you use it) Just calculate a middle time for 5-10 calls.
  1. slow UI drawing
  • check your RecyclerView or other UI. Use AndroidStudio profiler to find out potential slow code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM