如何同步执行 RoboSpice 请求?是否可以?我想在 IntentService 中使用 RoboSpice。
编辑:
有时需要同步执行一些东西,例如在服务中。在我最近的项目中,我必须对一些不同类型的请求进行排队,并且我想将 IntentService 与 RoboSpice 一起使用。在我的情况下,当我执行不同的请求时,我需要等待来自 request1
的结果然后将数据从它传递给 request2
并执行它。
我想要某种批处理请求队列。假设我们有两种类型的请求:request1
和 request2
. request2
需要 request1
获取的数据:
request1
request1
获取的数据并转至 request2
request2
我想使用 IntentService(队列),但由于异步,它在启动请求后死亡。
在我看来,使用监听器或 CountDownLatch 不是最好的方法。
最佳答案
由 Riccardo Ciovatti 提出 on the RoboSpice mailing list ,您的问题的直接答案是:
final CountDownLatch latch = new CountDownLatch(1);
final YourRequest request = new YourRequest();
spiceManager.execute(request, new RequestListener<YourResponse>() {
@Override
public void onRequestFailure(SpiceException spiceException) {
latch.countDown();
}
@Override
public void onRequestSuccess(YourResponse response) {
latch.countDown();
}
});
latch.await();
但这不是一个好的习惯用法,除非它是在后台线程上执行的。异步处理是 RoboSpice 的一个基本特性;它使 UI 线程保持空闲。
关于robospice - 如何同步执行 RoboSpice 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23243125/