问题描述
我正在使用MVVM体系结构制作一个android应用.我想从API提取数据并将其插入到会议室数据库中,然后从我的应用程序中的会议室取得数据.我不知道这是否是实现数据库缓存功能的更好方法.如果还有其他更好的方法,请告诉我.
I am making an android app using MVVM architecture. I want to fetch data from an API and insert it into room database and then fetch it from the room in my app. I don't know if it is a better way to implement database cache functionality. If there is some other or better way to do so please let me know.
推荐答案
您的问题确切说明了如何使用Room:
your question explains exactly how you would use Room:
- 从api获取数据
- 将其插入房间数据库
- 从我的应用程序的房间中获取它
Room允许您使用Rxjava之类的东西在本地存储数据并使用各种不同的线程检索此数据,还可以让您观察LiveData的任何变化,Room绝对是考虑进行缓存的不错的选择
Room allows you to store your data locally and retrieve this data with various different threads, making use of something like Rxjava, it also allows you to observe onto any changes with LiveData, Room is definitely a decent option to consider for caching
但是
使用Room是几种不同的缓存实现之一,请考虑发布代码或特定方案以获得更好的答案.编码愉快.
Using Room is ONE of SEVERAL different implementations of caching, consider posting code or a specific scenario for better answers. Happy coding.
使用Room(或任何缓存)的一种常见方法是从数据库加载初始数据,向用户显示初始数据,执行api调用,更新缓存并从api显示此更新的数据
A common approach to using Room (or any caching) would be to either load initial data from the database, display this to the user, perform an api call, update the cache and display this updated data from the api
OR
如果用户没有Internet连接,只需将其在缓存中可用的内容用作数据.同样,所有这些都取决于您的特定情况.
If the user does not have an internet connection, simply use what he has available in the cache as data. Again, all of this depends on your specific scenario.
这篇关于如何使用会议室数据库作为缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!