[英]Android MVVM ROOM Single Sources of Truth Question
About Single Sources of Truth Google document said:关于真相的单一来源谷歌文件说:
Using this model, the database serves as the single source of truth, and other parts of the app access it using our UserRepository.
使用此模型,数据库充当单一事实来源,应用程序的其他部分使用我们的 UserRepository 访问它。 Regardless of whether you use a disk cache, we recommend that your repository designate a data source as the single source of truth for the rest of your app
无论您是否使用磁盘缓存,我们都建议您的存储库将数据源指定为应用程序其余部分的单一事实来源
https://developer.android.com/jetpack/guide?gclid=CjwKCAjwo4mIBhBsEiwAKgzXOH1Pq--Ws1PLzUiSP4RmDE6ByKfEi6mdXu5g86btqveIdJvvrgYuxBoCz8wQAvD_BwE&gclsrc=aw.ds#connect-viewmodel-repository https://developer.android.com/jetpack/guide?gclid=CjwKCAjwo4mIBhBsEiwAKgzXOH1Pq--Ws1PLzUiSP4RmDE6ByKfEi6mdXu5g86btqveIdJvvrgYuxBoCz8wQAvD_BwE&gclsrc=aw.ds#connect-viewmodel-repository
According to the document I save all data when I fected data from remote server and I only get data from room When I need to use in acitivty(In fact I collect flow which is defined in viewmodel).根据文档,当我从远程服务器感染数据时,我保存了所有数据,并且我只在需要在活动中使用时从房间获取数据(实际上我收集了在 viewmodel 中定义的流)。
It seems so good!看起来太好了! It avoids the different data sources mix up together!
它避免了不同的数据源混淆在一起! But actually I found some strange question gradually:
但实际上我逐渐发现了一些奇怪的问题:
In my App, I have a list that the server may change it(Because we have data manager website that admin can update or delete data).在我的应用程序中,我有一个服务器可能会更改它的列表(因为我们有管理员可以更新或删除数据的数据管理器网站)。 So in order to get the newest list data from server, I must clear all data stored in room and fect data again from remote server.
因此,为了从服务器获取最新的列表数据,我必须清除存储在房间中的所有数据并再次从远程服务器感染数据。 This operation seems redundant: "why could I get data directly from remote server", I mean, I only get data from remote source is also a single sources truth.
这个操作似乎是多余的:“为什么我可以直接从远程服务器获取数据”,我的意思是,我只从远程源获取数据也是单源的道理。 And also it cause a promble: my app will flash a moment because clear data make list empty and fect data from server make list full!
而且它也会引起问题:我的应用程序会闪烁片刻,因为清除数据会使列表为空,而来自服务器的 fect 数据会使列表变满!
The most important thing is that it seems like the local data is not necessary because I must stay the newest list from remote server.最重要的是,似乎不需要本地数据,因为我必须保留来自远程服务器的最新列表。 Some people may say that save data into room can make us app available offlice.
有人可能会说,将数据保存到房间可以使我们的应用程序在办公室可用。 I agree that, But in this place, my item of list is represent a image url, and after click the item, the app will jump to a new activity and display a ImageView base on the url we get from the list.
我同意,但是在这个地方,我的列表项代表一个图像 url,单击该项后,应用程序将跳转到一个新的活动,并根据我们从列表中获取的 url 显示一个 ImageView。 If app offlice, the ImageView couldn't load the url also.
如果 app offlice,ImageView 也无法加载 url。
I am so confused I couldn't load all image url(use base64-url to avoid load invalid) in a moment also, because the data is so much.我很困惑,我也无法立即加载所有图像 url(使用 base64-url 以避免加载无效),因为数据太多了。 And if I say I need a search function in this list and I need load so much unbelievable data into my room, It seems so unreal and event fantasy!
如果我说我需要这个列表中的搜索功能,我需要将这么多令人难以置信的数据加载到我的房间中,这似乎太不真实和事件幻想!
In brief:简单来说:
Room is a nessary?房间是必需品吗? Couldnt just fect data from remote?
不能只影响远程数据吗?
If room is nessary, how to solve problem I met, do my incorrect useage cause the problem?如果房间需要,如何解决我遇到的问题,我不正确的使用会导致问题吗?
Hi @psycongroo as I Understood your problem, and I want to share my experience:嗨@psycongroo,我理解了你的问题,我想分享我的经验:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.