Search code examples
androidsqlinsertandroid-contentprovider

Android contentprovider insert takes a lot of time


I made my own contentprovider where I put a lot of data in at once with multiple inserts.

The app will receive the data from an external source and at this moment I receive about 30 items (therefor 30 times an insert).

Now I noticed that this takes a lot of precious time (about 3 seconds, 100ms on each insert).

How can I improve the speed of the contentprovider? I already tried to bulkInsert them all together but them it will take up to 5 sec.

Thanks in advance.


Solution

  • wrap all that in insertBulk into transactions.

    Example:

        SQLiteDatabase sqlDB = mDB.getWritableDatabase();
        sqlDB.beginTransaction();
        try {
    
            for (ContentValues cv : values) {
                long newID = sqlDB.insertOrThrow(table, null, cv);
                if (newID <= 0) {
                    throw new SQLException("Failed to insert row into " + uri);
                }
            }
            sqlDB.setTransactionSuccessful();
            getContext().getContentResolver().notifyChange(uri, null);
            numInserted = values.length;
        } finally {
            sqlDB.endTransaction();
        }
    

    bulkInsert does not use transactions by default, since the default behavior just calls insert:

    Override this to handle requests to insert a set of new rows, or the default implementation will iterate over the values and call insert(Uri, ContentValues) on each of them.