I make requests in a cycle:

for (int j=0;j<=15;j++) { Log.d(mytag,"111111"); ... VKRequest request = VKApi.audio().search(VKParameters.from(VKApiConst.Q, artist, VKApiConst.OFFSET, 100, VKApiConst.COUNT, 100)); request.executeWithListener(new VKRequest.VKRequestListener() { @Override public void onComplete( VKResponse response ) { super.onComplete(response); Log.d(mytag,"222222"); ... } } } 

First displayed 15 times.

111111

then several times

222222

That is, the for loop first runs 15 times, and only after that the query is executed and worked with it, although it should occur during the for loop.

  • four
    That's right, because the execution of the request takes some time. - s8am

1 answer 1

You sequentially launch asynchronous requests to the server. in 99. (9)% of cases they are executed longer than the cycle itself is executed.

Because your task is asynchronous, i.e. executed in a thread other than a loop thread, the next loop iteration will be executed regardless of the process of the asynchronous task.

If you need to consistently perform tasks, then you either need to perform them in the same thread as the cycle, or start the next task at the moment of the end of the current one. To do this, there are callbacks of the type onSuccess, of which the following task must be run