How to execure RoboSpice requests synchronously? Is it possible? I would like to use RoboSpice in IntentService.
Edit:
From time to time comes the need to execute something synchronously e.g. in service. In my recent project I have to queue some different types of requests and I wanted to use IntentService with RoboSpice. In my case when I'm executing different requests I need to wait for results from request1
then pass data from it to request2
and execute it.
I want to have some kind of batch request queue. Lets say we have two types of request: request1
and request2
. request2
needs data fetched by request1
:
request1
request1
and pass to request2
request2
I wanted to use IntentService (queue) but it dies after starting requests because of async.
In my opinion using listeners or CountDownLatch isn't the best way.
As proposed by Riccardo Ciovatti on the RoboSpice mailing list, a direct answer to your question is :
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();
But this isn't a good idiom to use unless it's being executed on a background thread. Asynchronous processing is an essential feature of RoboSpice; it keeps the UI thread free.