使用工作管理器更新网络请求的逻辑 [英] Logic to update network requests using Work Manager

查看:61
本文介绍了使用工作管理器更新网络请求的逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从API提取数据的应用程序.所以基本上,现在,该应用程序的工作方式如下:

I have an application which fetches data from an API. SO basically, right now, the app works as such:

  • 如果已连接到互联网,请获取数据并使用Android Room存储以供离线使用
  • 如果未连接到Internet,请检查Room中是否存在数据.如果存在,则显示它.如果不存在,则显示错误消息.

我在线研究了如何实施有效的离线存储策略,Google建议使用Work Manager对请求进行排队,然后在连接后发送请求.

I did some research online on how to implement an efficient offline storing policy and Google suggests to use Work Manager to queue requests and then send it when connected.

我实际上想知道如何实现此目标?(不是代码,而是逻辑,也就是说,我应该每天将请求调度到API还是每次连接到Internet时?)

I actually want to know how to implement this ? (not the code but the logic, i.e should i schedule requests everyday to the API or every time it's connected to the internet ?)

如果有离线应用程序经验的人可以提供帮助,那就太好了.

If someone with experience with offline apps could help would be great.

我的网络请求是通过Retrofit完成的,我已经创建了一个类,该类执行对API的调用.

My network requests are done through Retrofit and i already create a class that perform calls to the API.

推荐答案

请牢记WM(工作管理器)旨在在满足某些条件(例如:用户电量充足,显示器关闭等)时执行操作.).因此,这可能最终导致您的数据在需要时不被更新.WM对于您想发生的操作很有用,但对于立即"发生并不重要.我会说总是将Room DB用作事实的唯一来源.如果数据在房间中,请显示它;如果不是,请获取它;如果不能,请尝试.向用户发送消息.您可以使用 NetworkConnectivityListener 来监视连接性,并检查是否有待处理的查询(可以将此查询的参数存储在Room数据库的另一个表中,以方便使用).因此,您将查询数据库,获取待处理的查询(如果有)并执行它们,更新数据,然后让ViewModel/Repository决定是否有上下文显示此数据(UI).

Keep in mind WM (work manager) is designed to perform operations when certain conditions are met (e.g.: the user has enough battery, the display is off, etc.). So this may end up with your data not being updated when you need it. WM is good for operations you want to happen but are not critical to occur "right now". I'd say always use the Room DB as the single source of truth. If the data is in room, show it, if it's not, fetch it, if you can't, well, you tried. Send a message to the user. You can use a NetworkConnectivityListener to monitor connectivity and check if you have a pending query (you could store the parameters of this query in your Room database in another table for ease of use). So you'd query the DB, obtain the pending queries (if any) and execute them, update the data, and let the ViewModel/Repository decide if there's a context to show this data (UI).

我觉得您已经很接近实现您的需求了.

I feel like you are very close to achieve what you need.

换句话说:

UI:对于某些密封类xxx 状态,请观察其viewModel以告知其操作(显示空列表,显示错误,将数据传递给recyclerview适配器等).

UI: Observes its viewModel for some sealed class xxx state to tell it what to do (show an empty list, show an error, pass data to a recyclerview adapter, etc.).

ViewModel:使用其 viewModelScope.launch {...} 将调用 repository.fetch(...)或类似的代码.当Fragment告诉它这样做(例如,用户按下按钮)或某个生命周期事件(例如onStart)时,您的viewModel将获取此数据.

ViewModel: Using its viewModelScope.launch { ... } will call a repository.fetch(...) or similar. Your viewModel will fetch this data when the Fragment tells it to do so (e.g. the user pressed a button) or on some lifecycle event (onStart for example).

在这种情况下,存储库通常公开一个 flow (如果您可以使用实验性api)或可以执行以下操作(取决于您的业务规则而有所不同)的暂停功能>

The Repository in this case normally exposes a flow (if you can use the experimental api) or a suspend function that can perform the following actions (that can vary depending on your business rules)

  1. 如果数据库中有可用数据,请立即将其返回.
  2. 如果数据很旧(或者我们仍然想刷新它),请执行网络API(如果有连接的话).如果没有连接,则可以存储此待处理"邮件,在数据库中查询以备后用.您还可以在执行任何上述操作之前检查是否有待处理的查询,它可能已过时,或者您需要执行该查询.
  3. 无论如何,一旦查询通过,就将结果插入数据库中,并调用在步骤1中使用的相同方法.
  4. 不要忘记更新您的待处理"邮件,查询您是否有一个(或是否使用了这个).

借助WorkManager,您可以安排从API提取数据"部分事情会在某个时候发生(这样您的数据将保持最新),但是我真的真的取决于您的用例.

With WorkManager, you could schedule the "fetch data from API" part to happen at some point (so your data will be kept more up to date), but I all really depends on the use-cases you have.

这篇关于使用工作管理器更新网络请求的逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆