简体   繁体   English

Android MVVM ROOM 单一事实来源问题

[英]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,我理解了你的问题,我想分享我的经验:

  1. You can handle any error with loading URL with placeholder I mean if you got an error with no Internet connection user will see placeholder, but in general libs like Picasso or Glide can cache images when it`s load one time, so the user will see the Image.您可以使用占位符处理加载 URL 的任何错误我的意思是,如果您在没有 Internet 连接的情况下遇到错误,用户会看到占位符,但一般来说,像 Picasso 或 Glide 这样的库可以在加载一次时缓存图像,所以用户会看到图片。
  2. The question about why we need to use room instead of fetch data from remote directly.关于为什么我们需要使用房间而不是直接从远程获取数据的问题。 So from your question I don`t understand why you need to drop your local changes even they are completely new, user can have a low internet connection so he will see an empty list instead of previous data with for example progress indicator.所以从你的问题我不明白为什么你需要放弃你的本地更改,即使它们是全新的,用户的互联网连接可能很低,所以他会看到一个空列表而不是以前的数据,例如进度指示器。 And also if the user doesn't have the internet at all you can show some dialog to explain what the problem but old data is still present.此外,如果用户根本没有互联网,您可以显示一些对话框来解释问题所在,但旧数据仍然存在。 If you are using, for example, RecyclerView you can update data with Paging 3 from google, and they update the only necessary items from your list.例如,如果您使用 RecyclerView,您可以使用 Google 的 Paging 3 更新数据,它们会更新您列表中唯一必要的项目。 PS let me know if that help, or you have another question. PS让我知道是否有帮助,或者您还有其他问题。

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

相关问题 结合 RxJava MVVM 中的不同来源(FCM、网络、ROOM) - Combining different sources in RxJava MVVM (FCM, Networking, ROOM) 从后台线程中的 Room 查询中获取单个 object 并在视图中使用它。 安卓/科特林/MVVM - Get a single object from a Room query in a background thread and use it in the view. Android/Kotlin/MVVM Android MVVM 存储库和 ViewModel 问题 - Android MVVM Repository and ViewModel Question Android Room SQLITE 使用 MVVM 更新自定义查询 - Android Room SQLITE Update Custom Query with MVVM Android Room with LiveData + ViewModel 刷新问题 - Android Room with LiveData + ViewModel Refresh Question Android MVVM + 数据绑定 + 架构组件(ViewModel 和 Room)示例 - Example of Android MVVM + Databinding + Architecture Components (ViewModel and Room) 如何在mvvm模式中有一个闪屏,在Android中有空间? - how to have a splash screen in mvvm pattern with room in android.? 如何使用 MVVM 架构在 Room Android 中获取数据 - How to fetch data in Room Android using MVVM architecture 如何使用 MVVM 架构上的 Room 在 android 存储库 class 中查询? - How to query inside android repository class using Room on MVVM architechture? Android MVVM + Room 通过其他 LiveData 对象创建 LiveData RecyclerViewItem 对象 - Android MVVM + Room creating LiveData RecyclerViewItem objects by other LiveData objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM