Andrew
Andrew

Reputation: 397

android and sqlite: sometimes insert lasts over 900 ms

I'm using the well documented code to insert data in my sqlite database

ContentValues cv = new ContentValues();
cv.put("timestamp", timestamp);
cv.put (..., ...);

db.insert("datatable", "id", cv);

This lasts usually about 50ms, but afters some inserts it lasts over 900 ms and goes back to 50ms. Because the db inserts are done in the main thread, the long inserts (over 900ms) blocks my UI.

Has someone an idea to avoid such long inserts?

Upvotes: 0

Views: 177

Answers (2)

Graham Borland
Graham Borland

Reputation: 60691

Doing this on the main thread is a bad idea, but to answer the specific question:

Has someone an idea to avoid such long inserts?

If you're doing multiple inserts at once, then make sure you wrap them all inside a transaction. This can have a dramatic effect on performance.

Upvotes: 3

user
user

Reputation: 87064

You shouldn't be doing those operation on the main UI thread. Spawn a new thread or use an AsyncTask.

Upvotes: 1

Related Questions