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());

相關問題