robospice - 如何同步执行 RoboSpice 请求?

标签 robospice

如何同步执行 RoboSpice 请求?是否可以?我想在 IntentService 中使用 RoboSpice。

编辑:

有时需要同步执行一些东西,例如在服务中。在我最近的项目中,我必须对一些不同类型的请求进行排队,并且我想将 IntentService 与 RoboSpice 一起使用。在我的情况下,当我执行不同的请求时,我需要等待来自 request1 的结果然后将数据从它传递给 request2并执行它。

我想要某种批处理请求队列。假设我们有两种类型的请求:request1request2 . request2需要 request1 获取的数据:

  • 执行 request1
  • 等待
  • 获取由 request1 获取的数据并转至 request2
  • 执行 request2
  • 等待
  • 转到 1。

  • 我想使用 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/

    相关文章:

    android - 如何使用 Mockito 沿 Robospice 和 Retrofit 测试 API?

    android - 如何正确使用com.octo.android.robospice来缓存位图

    android - Android 上使用 Spring 上传 RoboSpice 文件

    android - 使用 ORMLite 在 Robospice 中缓存嵌套对象

    android - 我如何让 Robospice 将来自 Retrofit 和 OKHttp 的 200 响应以外的任何其他内容视为错误

    android - 用于非 Maven 模式的 Robospice jar

    android - 在 RetrofitError 中捕获错误并将其传递给 RoboSpice Error

    android - 使用 RoboSpice 进行异步线程

    android - 新 Activity 的 onStart 在父 Activity 的 onStop 之前被调用