-1

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?

ZakTaccardi
  • 12,212
  • 15
  • 59
  • 107

2 Answers2

1

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

dumazy
  • 13,857
  • 12
  • 66
  • 113
0

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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115