gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:38:14 27 4
gpt4 key购买 nike

如何同步执行 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/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com