将 REST 调用与“Activity"分离的最佳方法 [英] Best way to decouple REST call from `Activity`

查看:29
本文介绍了将 REST 调用与“Activity"分离的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Retrofit 并希望 REST 调用在用户在我的活动之间导航时继续存在(而不是仅仅在配置更改时保留它们).几个选项:

I am using Retrofit and want REST calls to survive when the user navigates between my activities (as opposed to just retaining them accross configuration changes). Couple of options:

  1. AsyncTask 中改造同步调用并将其放入保留的 "任务片段"
  2. IntentService 和广播
  3. Google 建议每个应用的单例,至少使用 Volley...
  1. Retrofit syncronous call within an AsyncTask and put that in a retained "task fragment"
  2. Retrofit syncronous call within an IntentService and broadcast
  3. Google suggests a per-app singleton, at least with Volley...

最好的方法是什么?我可以将 RxJava 视为一种 Retrofit 模式,但即使有人只是建议最迟在 onDestroy 中取消订阅返回的 Observable" - 所以不是一个真正的解决方案,尽管对新的内置 Rx 支持大肆宣传.

What is the best approach? And I can see RxJava as a Retrofit mode but even there one is just adviced to "unsubscribe from the returned Observable in onDestroy at the latest" - so not a real solution, despite all hype about the new built-in Rx support.

推荐答案

我认为保留的片段不会在这里实现您想要的.它可以帮助处理配置更改,但不能帮助处理活动之间的更改.

I don't think a retained fragment will achieve what you want here. It can help with handling configuration changes, but not between activities.

你可能应该把你的问题分成两个问题,一个是从哪里开始网络调用,另一个是接收响应.

You probably should break your problem into 2 problems, one is where to start the network call, and other is receiving the response.

对于第一个问题,可以正常启动Activity/Fragment中的网络调用.但是,如果您要经常这样做,您可能希望管理服务中的调用并使用某种队列.

For the first problem, you can start the network call in the Activity/Fragment normally. If you're going to do this a lot though, you'll probably want to manage the calls in a Service and use some sort of queue.

要接收响应,您应该确保您的侦听器(回调或订阅者)未绑定到您的 Activity/Fragment 实例(对嵌套类或顶级类使用静态).然后,您可以使用广播或 EventBus/Otto 将结果转发到 Activity.

To receive the response, you should just make sure your listener (Callback or Subscriber) isn't tied to your Activity/Fragment instance (using static for nested classes, or top-level classes). You can then forward the result to the Activity using a broadcast or EventBus/Otto.

这可以在您的回调/订阅者中处理,也可以在服务上下文中处理.(虽然如果你想在 Activity 中使用 Broadcast,你应该引用 Context.getApplicationContext() 而不是当前的 Activity 上下文,以防你的 Activity 被销毁)

This can be either handled in you Callback/Subscriber, or inside Service context. (Though if you want to use Broadcast inside Activity, you should reference Context.getApplicationContext() instead of the current Activity context, in case your Activity is destroyed)

执行此操作后,您的网络调用应该会正常恢复而不会出错,即使请求它们的 Activity 已停止.

Doing this your network calls should resume normally without errors, even when the Activity that requested them is stopped.

最后一步是缓存响应,因为如果 Activity 停止,那么响应将无处可去.网络调用虽然完成了,但还是浪费了.您应该以某种方式缓存这些响应,以便您的 Activity/Fragment 可以在重新创建时检索它们.

The final step would be to cache the response, because if the Activity is stopped, then the response will go no where. Even though the network call was completed, it was wasted. You should cache these responses in some way so your Activity/Fragment can retrieve them upon recreation.

我个人会选择 Service 与 EventBus 组合.

Personally I would go for Service with EventBus combination.

这篇关于将 REST 调用与“Activity"分离的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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