简体   繁体   English

如何提高房间查询时间

[英]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.我在我的应用程序中成功使用了 Android Room 和 LiveData 组合,但我注意到它在查询和将数据呈现给 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.这不是世界末日,也不是不可持续的,但是当打开其他消息应用程序(如 WhatsApp)时,差异是巨大的。 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.当我打开这样的应用程序时,对话和消息已经可用,甚至没有时间在 RecyclerView 列表中通常可用的默认 animation 中播放淡入淡出。

On the other hand in my app I can clearly see the delay the data takes until it is presented to the UI.另一方面,在我的应用程序中,我可以清楚地看到数据在呈现给 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).我知道将视图模型附加到片段意味着每次重建关联的片段时都会重建相同的视图模型,因此理论上将该视图模型附加到活动将意味着数据在 memory 中可以存活更长时间,从而在重建时节省加载时间片段,但是它第一次加载时仍然会显示明显的延迟,所以这不是一个理想的解决方案,我也不知道这是否是一个不明智的修改(将片段的视图模型附加到活动生命周期而不是片段的) .

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?也许 WhatsApp 使用的是完全不同的 SQLite 库,它比 Room 快一点,所以我永远无法使用 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.中间件转换(如果你使用它)只需计算 5-10 次调用的中间时间。
  1. slow UI drawing缓慢的 UI 绘制
  • check your RecyclerView or other UI.检查您的 RecyclerView 或其他 UI。 Use AndroidStudio profiler to find out potential slow code.使用 AndroidStudio 分析器找出潜在的慢代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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