2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

bulkInsertとapplyBatchのアトミック性を保証する

Posted at

デフォルトでは、bulkInsert,applyBatch共にアトミック性は保証されていません。
詳細:http://yuki312.blogspot.jp/2012/07/androidbulkinsertapplybatch.html

bulkInsert
public int bulkInsert(Uri uri, ContentValues[] values) {
    SQLiteDatabase db = MyDataBase.getInstance(getContext())
            .getWritableDatabase();
    db.beginTransaction();
    try {
        SQLiteStatement insertStmt = db
                .compileStatement("INSERT INTO test "
                        + "(name) VALUES (?);");
        for (ContentValues value : values) {
            insertStmt.bindString(1, value.getAsString("name"));
            insertStmt.executeInsert();
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
    ...
applyBatch
@Override
public ContentProviderResult[] applyBatch(
        ArrayList<ContentProviderOperation> operations)
        throws OperationApplicationException {
    SQLiteDatabase db = MyDataBase.getInstance(getContext())
            .getWritableDatabase();
    db.beginTransaction();
    try {
        ContentProviderResult[] result = super.applyBatch(operations);
        db.setTransactionSuccessful();
        return result;
    } finally {
        db.endTransaction();
    }
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?