Android when to use ContentResolver applyBatch or BulkInsert
Asked Answered
A

2

12

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.

Ame answered 20/2, 2012 at 15:17 Comment(0)
V
12

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.

Vitrify answered 2/3, 2012 at 13:47 Comment(0)
N
5

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.

Nocturn answered 29/8, 2014 at 14:16 Comment(1)
If you are creating your own ContentProvider class, you can override bulkInsert so that transactions are used.Intratelluric

© 2022 - 2024 — McMap. All rights reserved.