項目中遇到處理批量插入數據的功能。後期提高了插入數據效果,用事務的方式。
在ContentProvider 中重寫方法:
@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation>operations)
throws OperationApplicationException{
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
db.beginTransaction();//開始事務
try{
ContentProviderResult[]results = super.applyBatch(operations);
db.setTransactionSuccessful();//設置事務標記爲successful
return results;
}finally {
db.endTransaction();//結束事務
}
}
利用ContentProviderOperation ,ContentResolver的applyBatch即可。
感謝同事牛人指點。O(∩_∩)O~