Android的时候使用ContentResolver的applyBatch或BulkInsert时候、Android、ContentResolver、BulkInsert

2023-09-07 16:32:26 作者:走下去走上来

现在我的应用程序时,我想改变数据的ContentProvider我,我只是用INSERT,UPDATE的ContentResolver的方法,和删除。但一对夫妇在Android SDK示例项目,我注意到他们使用applyBatch或BulkInsert。所以我想知道何时使用这些方法之一,什么是他们使用了什么我现在做的优势。

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.

推荐答案

内容提供商可以有观察员,如游标,每次都通知插入,更新或删除发生。通常,这会导致一些工作正在做更新UI。当你有多个操作的同时应用,这可能导致国家的观察员重复更新。一般情况下,如果你有多个插入,更新或删除来执行,这是更有效地做他们是散装的。

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.

话虽这么说,默认ContentProvider.applyBatch()方法只在一批迭代,并将它们应用于个别进不去。该ContentProvider的的作家必须重写此,更有效地应用它采取批处理操作的优势。

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.