1

我需要执行一大批操作。为了避免阻塞,我需要在执行批处理时多次执行数据库事务。我已阅读articleContentProviderOperation.withYieldAllowed()。这完全适合我的情况。ContentProviderOperation.withYieldAllowed()它甚至可以工作吗?

我不明白的是:如果android应该提交交易,如果ContentProvider.applyBatch()方法不处理交易?当文档(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

请仔细阅读,

当你使用一个ContentProviderOperation并有插入,更新或删除多条记录,该方法withYieldAllowed()就派上用场了。

嵌套类ContentproviderOperation.Builder的withYieldAllowed()方法处理此问题。唉,这个方法做的事情没有记录在课堂上。

+0

那么,它是否会导致ContentProvider提交任何事务,因为它应该或不? – Gus

0

按照以下代码

的ArrayList()OPS =新

的ArrayList();

ops.add(ContentProviderOperation newInsert(RawContacts.CONTENT_URI)

.withValue(RawContacts.ACCOUNT_TYPE, someAccountType) 

.withValue(RawContacts.ACCOUNT_NAME,someAccountName) .build());

相关问题