Search code examples
androidperformancesqliteandroid-contentproviderandroid-contentresolver

Android - contentResolver.notifyChange() slowing performance


I have a MyContentProvider class that overrides bulkInsert(). Within this method, I use a SQLite transaction to insert about 4,000 rows into the database, which takes about 25 seconds on a Samsung Galaxy S4 device.

However, when I remove this line from my bulkInsert() method...

getContext().getContentResolver().notifyChange(insertedId, null);

...the total insertion time drops to about 1 or 2 seconds.

So, is there a better way to call notifyChange()?

I have tried calling it in another thread, like this...

                    new Thread(new Runnable() {
                    public void run() {
                        getContext().getContentResolver().notifyChange(insertedId, null);
                    }
                }).start();

...but it's still slow and, for some reason, results in an OutOfMemoryError.

For completeness, here is my bulkInsert() method...

    @Override
public int bulkInsert(Uri uri, ContentValues[] valuesArray) {

    /*
     *  Open a read / write database to support the transaction.
     */
    SQLiteDatabase db = dbHelper.getWritableDatabase();

    String tableName;

    switch (uriMatcher.match(uri)) {
        case BRANDS_SEARCH:
            tableName = BRAND_NAMES_TABLE;
            break;
        case PRODUCTS_SEARCH:
            tableName = PRODUCTS_TABLE;
            break;
        case PARENT_COMPANIES_SEARCH:
            tableName = PARENT_COMPANIES_TABLE;
            break;
        case PRODUCTS_DATA_SEARCH:
            tableName = PRODUCTS_DATA_TABLE;
            break;
        default:
            //break;
            throw new IllegalArgumentException("Unsupported URI: " + uri);
    }

    /*
     * Begin the transaction
     */
    db.beginTransaction();

    int numSuccessfulInsertions = 0;

    try {

        for (int i = 0; i < valuesArray.length; i++) {

            /*
             *  Insert the values into the table
             */
            long rowId = db.insert(tableName, null, valuesArray[i]);

            if (rowId > -1) {

                /*
                 * Increment numSuccessfulInsertions
                 */
                numSuccessfulInsertions++;

                /*
                 *  Construct the URI of the newly inserted row.
                 */
                Uri insertedId = ContentUris.withAppendedId(uri, rowId);

                /*
                 *  Notify any observers of the change in the data set.
                 */
                getContext().getContentResolver().notifyChange(insertedId, null);



            }
            else {

                /*
                 * Don't give up (as not all insert attempts need to succeed)
                 */
                //throw new Exception("Could not insert row");
            }

        }

        /*
         * Return number of successful insertions
         */
        return numSuccessfulInsertions;
    }
    catch(Exception e) {

        Log.e(LOG_TAG, "bulkInsert exception", e);

        /*
         * Return number of successful insertions
         */
        return numSuccessfulInsertions;

    }
    finally {

        /*
         * Some (or all) insertion attempts succeeded
         */
        db.setTransactionSuccessful();

        /*
         * Always end the transaction
         */
        db.endTransaction();
    }
}

Solution

  • Notify once for the bulk operation, not once for each record inserted. Move your call to notifyChange such that it follows the for loop.