提高android ContentProvider的效率
2013-01-25 11:54
295 查看
在自己的ContentProvider类里,重写applyBatch方法,加入事务:
在处理数据时使用ContentProviderOperation:
然后循环加入数据库操作:
然后提交操作:
这样,用了事务之后,数据库效率会大大提升。
@Override public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation>operations) throws OperationApplicationException{ SQLiteDatabase db = mOpenHelper.getWritableDatabase(); db.beginTransaction(); try{ ContentProviderResult[]results = super.applyBatch(operations); db.setTransactionSuccessful(); return results; }finally { db.endTransaction(); } }
在处理数据时使用ContentProviderOperation:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
然后循环加入数据库操作:
ContentValues value = new ContentValues(); ops.add(ContentProviderOperation.newUpdate(MyProvider.CONTENT_URI) .withSelection("_id='" + entry.getId() + "'", null) .withValues(value) .withYieldAllowed(true) .build());
然后提交操作:
try { mContext.getContentResolver().applyBatch(MyProvider.AUTHORITY, ops); } catch (RemoteException e) { e.printStackTrace(); } catch (OperationApplicationException e) { e.printStackTrace(); }
这样,用了事务之后,数据库效率会大大提升。
相关文章推荐
- [转]提高android ContentProvider的效率
- 提高android ContentProvider的效率
- 提高android ContentProvider的效率
- Android开发之提高程序效率
- 9个Web工具提高Android开发效率
- Android分布式编译学习(二)使用ccache 提高编译效率 —— Ubuntu12.04上部署distcc分布式编译
- android图片的缓存--节约内存提高程序效率
- android平台下提高sqlite大容量数据存储效率
- 使用WEB工具快速提高Android开发效率
- 安卓开发如何提高Android程序的效率(转)
- 如何提高android程序运行效率,程序员在开发过程中应当注 意的一些事项
- 提高android应用的效率--主要讲解listview的优化
- Android如何提高ListView的滑动效率
- 安卓开发如何提高Android程序的效率(转)
- 使用IntelliJ IDEA(androidstudio前身)开发android移动应用前的基本设置,提高开发效率
- 提高效率,Android布局中viewHolder的应用
- 提高Android开发效率的9个Web工具
- [Android] 提高ORMLite插入大量数据效率的解决方案
- 提高Android开发效率的9个Web工具
- 提高eclipse使用效率 提高Android开发效率的小技巧