ContentProvider bulkInsert嵌套事务,需要插入和更新

时间:2013-09-27 12:50:13

标签: android android-sqlite android-contentprovider

我实现了ContentProvider bulkInsert()方法在事务中的表中插入大量行,但之后我需要在另一个表中进行更新,我只想在批量插入的情况下执行此操作承诺。这2个操作需要是原子的,无论是做还是不做,我该怎么做?

这是bulkInsert方法:

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

    int nrInserted = 0;
    String TABLE;

    int uriType = mUriMatcher.match(uri);


    switch (uriType){
        case FEEDS:
            TABLE = FeedsProviderContract.TABLE_NAME;
            break;
        default:
            throw new IllegalArgumentException( "Unknown URI: " + uri );
    }

    SQLiteDatabase db = mDbHelper.getWritableDatabase();

    //Begin inner transaction
    db.beginTransaction();

    try {

        for (ContentValues cv : values){

            db.insertOrThrow(TABLE, null, cv);

            nrInserted++;
        }

        db.setTransactionSuccessful();

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

    } catch (SQLException ex){

        ex.printStackTrace();

    }
    finally {
        db.endTransaction();
    }

    return nrInserted;
 }

现在当我打电话的时候,我能做出这样的外部交易吗?我想它不起作用。

...

//Begin outer transaction

getContentResolver().getContentProvider().bulkInsert(...);

//Update the other table

//End  outer Transaction

1 个答案:

答案 0 :(得分:0)

我需要的是ApplyBatch而不是BulkInsert,它现在可以按照我想要的方式工作。

相关问题