I'm writing android app, which uses some site api. This site provides sort of SDK to handle this api. I'm using it to make some requests to the site.
So, to make a request I should do like this:
VKRequestListener listener = new VKRequestListener(){
@Override
public void onComplete(VKResponse response){
// do smth when response is complete
}
}
VKRequest vkRequest = new VKRequest("request", "parameters");
vkRequest.executeWithListener(listener);
This listener
runs asynchronously, so if I make single request, all goes fine: I can handle it inside onComplete
method. But what to do if I need to make multiple requests, furthermore, each next request depends on previous response? I only see two ways, both look awful:
1. Make each next request in the onComplete
method. This seems to be too nesting and inflexible practice.
2. Make some kind of synchronous operation: after main thread executes request, check somehow if onComplete
executed (btw, how? with some kind of flag objects?) and if not, do Thread.sleep(1000)
, repeat this until response returned, than make another response.
So, tell me please if there are 'normal' way to handle multiple async requests?
0 comments:
Post a Comment