SQLite inserts timing out in Android?
During onCreate() I'm trying to insert about 20 rows into the SQLite database (开发者_JAVA百科these are just test values. For the final version I'll have to insert about 1000).
Using the debugger it appears that when running onCreate() this causes a "Activity pause timeout for HistoryRecord", and it seems from the log not all rows are inserted.
Is using an asyncronous task the only way to make this work without the timeout?
Thanks
Yes asyncronous task or in a separate Thread is the right way, additionally you can tell the db that you are starting a transaction which speeds up the process significantly for large inserts,
dh.getDb().beginTransaction();
//insert statemetns
dh.getDb().setTransactionSuccessful();
dh.getDb().endTransaction();
If it throws that warning with 20 records, imagine the delay with a thousand. Do you really want to block execution for so long?
Updating the database as an asyncronous task or in a separate Thread is the right way.
Using an asynchronous task is not the only way, but it is the best way. AsyncTask is also pretty easy. You DEFINITELY do not want to do this in the main thread.
精彩评论