Jordi Chacón
Jordi Chacón

Reputation: 1737

Is there a way to get all ongoing AsyncTasks?

I am writing a few tests for Android code that does business logic and triggers asynchronous db operations through the Sprinkles library. My tests are basically failing when they expect certain data to be present in db, but the data is missing due to the async writes not being completed yet. Therefore, for testing purposes, it seems that I need a way to wait for the async jobs to be finished before I do my assertions.

After taking a look at how Sprinkles does the asynchronous writes, it simply triggers a new AsyncTask. But these AsyncTasks are completely handled by Sprinkles so I do not have access to them. So I was wondering: is there a way to get hold of all ongoing AsyncTasks started by my application? If there is, then I can make my tests wait for all AsyncTasks to be completed. Not a great solution, but it would do the trick.

Otherwise, does anyone have any tips on how to tackle this problem?

Thanks a lot!

Upvotes: 0

Views: 59

Answers (2)

asaf gitai
asaf gitai

Reputation: 410

You can use

Map<Thread, StackTraceElement[]> m = Thread.getAllStackTraces();

then go over each stack traces and look for async tasks and see if they are in

doInBackground 

method

Upvotes: 1

Ahsan Saleem
Ahsan Saleem

Reputation: 43

I had to do something similar, I decided to keep Boolean variables to represent if they had completed or not. and check on the OnPostExecute method of every Async task if all had finished or not and if all had then set a boolean variable representing that.

Upvotes: 0

Related Questions