我有一个重写bulkInsert()的MyContentProvider类。在这种方法中,我使用SQLite事务向数据库中插入大约4,000行,在Samsung Galaxy S4设备上大约需要25秒。

但是,当我从bulkInsert()方法中删除此行时...

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

...总插入时间下降到大约1或2秒。

因此,有没有更好的方法来调用notifyChange()

我试图在另一个线程中调用它,像这样...

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


...但是它仍然很慢,并且由于某种原因导致生成OutOfMemoryError

为了完整起见,这是我的bulkInsert()方法...

    @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();
    }
}

最佳答案

对于批量操作,一次通知,对于插入的每个记录,一次通知。将您的调用移动到notifyChange,使其遵循for循环。

关于android - Android-contentResolver.notifyChange()降低性能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26781728/

10-08 21:26