我需要执行大批操作。为了避免阻塞,我需要在执行此批处理时多次开始并提交数据库事务。关于ContentProviderOperation.withYieldAllowed()
,我已经阅读了这个article。这完全适合我的情况。
我不明白的是:如果ContentProvider.applyBatch()
方法不处理事务,android应该如何提交事务?正如文档(API 23)声明获取事务行为一样,ContentProvider必须覆盖该方法。
/**
* Override this to handle requests to perform a batch of operations, or the
* default implementation will iterate over the operations and call
* {@link ContentProviderOperation#apply} on each of them.
* If all calls to {@link ContentProviderOperation#apply} succeed
* then a {@link ContentProviderResult} array with as many
* elements as there were operations will be returned. If any of the calls
* fail, it is up to the implementation how many of the others take effect.
* This method can be called from multiple threads, as described in
* <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
* and Threads</a>.
*
* @param operations the operations to apply
* @return the results of the applications
* @throws OperationApplicationException thrown if any operation fails.
* @see ContentProviderOperation#apply
*/
public @NonNull ContentProviderResult[] applyBatch(
所以这意味着我必须检查操作是否允许在我的applyBatch()
实现中自己产生并提交事务?这没有多大意义,我错过了什么?
答案 0 :(得分:0)
请仔细阅读,
每当您使用ContentProviderOperation并且必须插入,更新或删除许多记录时,使用YieldAllowed()的方法都会派上用场。
嵌套类ContentproviderOperation.Builder的withYieldAllowed()方法处理此问题。这个方法所做的事情没有在类本身上记录。
答案 1 :(得分:0)
按照以下代码
ArrayList()ops = new
的ArrayList();
ops.add(ContentProviderOperation。 newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, someAccountType)
.withValue(RawContacts.ACCOUNT_NAME,someAccountName) .build());