Android when to use ContentResolver applyBatch or BulkInsert

ぐ巨炮叔叔 提交于 2019-12-10 12:34:43

问题


Right now for my application when I want to alter data for my ContentProvider, I just use the ContentResolver methods of insert, update, and delete. But on a couple of sample projects in the Android SDK, I notice they use applyBatch or BulkInsert. So I want to know when to use either of these methods and what are the advantages of using them over what I'm doing now.


回答1:


Content Providers can have observers, such as Cursors, which are notified each time an insert, update or delete happens. Usually this results in some work being done to update the UI. When you have multiple operations to apply at the same time, this could result in repetitive updates by the observers. In general, if you have multiple insert, update or deletes to perform, it's more efficient to do them in bulk.

That being said, the default ContentProvider.applyBatch() method simply iterates over the batch and applies them individually anyhow. The writer of the ContentProvider must override this and apply it more efficiently to take advantage of the batch operations.




回答2:


Just to add more details about what jsmith said, is that the BulkInsert will not be transactional while the applyBatch yes, so if you want to make sure a group of operations are applied in a transaction way, use applyBatch.



来源:https://stackoverflow.com/questions/9363452/android-when-to-use-contentresolver-applybatch-or-bulkinsert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!