在主线程上进行回调 [英] Retrofit callback on main thread

查看:57
本文介绍了在主线程上进行回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拨打电话:

@GET("/user/{id}/data")
void getUserData(@Path("id") int id, Callback<Data> cb);

应该在主线程上执行回调(如果不使用RxJava).我的问题是:

Callback is supposed to be executed on the main thread (if not using RxJava). My questions are :

  1. 解析在哪里发生(假设我使用XML转换器进行流程响应).这是主线程,还是其他线程?是否取决于转换器的实现?
  2. 如果我必须包括一些(繁重的)验证规则/业务规则,是否需要在callable内产生一个新线程?还是可以在Callback方法中完成此操作?

我正在寻找从Web服务中获取活动数据的方法,从而避免自己进行线程管理(或使用其他方法(如IntentService等)),但又害怕使用RxJava(因为有实验性的支持)).是否存在另一种建议的方法来解决此问题?

I am looking for ways to get data in my activity from a web-service avoiding thread-management on my own (or using other approaches like IntentService, etc.), but am afraid of using RxJava either (because of experimental support). Is there another suggested approach to deal with this problem ?

推荐答案

解析在哪里发生(假设我使用XML转换器进行流程响应).这是主线程,还是其他线程?是否取决于转换器的实现?

Where does the parsing happen (let's assume I am using an XML converter for process response). Is this main thread, or a different one ? Does it depend on converter implementation ?

无论使用哪种转换器,始终都有后台线程.

Always a background thread no matter the converter you use.

如果我必须包括一些(繁重的)验证规则/业务规则,是否需要在callable内产生一个新线程?还是可以在Callback方法中完成此操作?

If I have to include some (heavy) validation rule/business rules, do I need to spawn a new thread inside callable ? Or is it fine to have it done in the Callback methods ?

这是相当主观的,有很多方法可以解决. Callback 将默认在主线程上执行.

This is fairly subjective and there's a lot of ways to tackle it. The Callback will be executed on the main thread by default.

您可以通过向 RestAdapter.Builder 提供自定义的 Executor 来更改将在其上调用回调的线程.但是,这将影响由该 RestAdapter 构建的所有服务,而这可能并不是您想要的.

You can change the thread on which callbacks will be invoked by supplying a custom Executor to the RestAdapter.Builder. This will affect all services constructed by that RestAdapter, however, which may not be what you want.

如果您要完成的工作可以与更新UI并行完成(例如,light-重量缓存).

There's nothing wrong with spawning another thread (or enqueueing on an executor) from the Callback if the work you want to be done can be done in parallel with updating the UI (for example, light-weight caching).

如果必须在通知UI之前完成昂贵的工作,则最好将方法切换为同步(无回调)并自己进行线程化.这样,您可以在HTTP调用之前和之后进行昂贵的操作(文件I/O,缓存,转换,验证等).

If the expensive work must be done before notifying the UI then you are better off switching the method to be synchronous (no callback) and doing the threading yourself. This way you can do expensive operations before and after the HTTP call (file i/o, caching, transforming, validating, etc.).

我们当前使用RxJava(其中Retrofit具有实验支持)来满足您的要求:

We currently use RxJava (of which Retrofit has experimental support) for what you are asking:

interface Foo {
  @GET("/")
  Observable<Foo> getFoo(String bar);
}

foo.getFoo()
  .mapMany(new ExpensiveOperationFunction())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(new Observer<TransformedFoo>() { .. });

这篇关于在主线程上进行回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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