Because of some poor decisions, I am stuck with an onCreate()
in my SQLiteOpenHelper
class that can run for up to 5 minutes. The user could turn off his phone, or force close the app during this process, meaning my onCreate() will be canceled prematurely. If that is the case - I need the database to be wiped and onCreate()
rerun. How can I do this?

- 12,212
- 15
- 59
- 107
-
What are you doing that makes it take up to 5 minutes? are you initializing data as well? – dumazy Apr 30 '15 at 15:23
-
initializing a database based on the files in the assets folder. – ZakTaccardi Apr 30 '15 at 15:25
2 Answers
Just create your tables in onCreate()
and initialise the data in another Service
or background thread.
In that background thread, call beginTransaction()
on your SQLiteDatabase
object before you initialise the data. Then call setTransactionSuccessful()
and endTransaction()
when it has finished.
Keep a flag in SharedPreferences
so you can check if this initialisation has already happened or not

- 13,857
- 12
- 66
- 113
You should use a transaction. If you finalize it (when the process is complete), all the data will be written onto the database.
Otherwise, they won't.
If you don't know what a transaction is, here is a tutorial on transactions: http://www.tutorialspoint.com/sqlite/sqlite_transactions.htm
In few words, a transaction allows you to write all data in a burst; if there is no finalization to a begun transaction, the data won't be written.
So, if any error occurs or your user forces a close, the transaction won't be finalized and no data will be written.

- 37,901
- 21
- 84
- 115