I have a class, DownloadAndSave
that extends from AsyncTask
. In its doInBackground
method, it retrieves data from an http connection and saves the data using OrmLite, but also cleans the old entries from the database. So, something like this:
doInBackground()
{
clearDb();
dataList = fetchDataFromHttp();
saveToDb(dataList);
}
I frequently get a DB exception:
attempt to re-open an already-closed object:SQLiteDatabase
in the clearDb() and saveToDb() functions.
And this is bad since old data from the previous call of DownloadAndSave
is mixed with the new data from DownloadAndSave
.
In my opinion, I need to make sure that when I start a thread, all of the other treads from the DownloadAndSave
class have finished, or in other words I need to run at most one instance of DownloadAndSave
at a time. So the question is: how do I make sure that only one instance of DownloadAndSave
will run in any point of time?