我一直在嘗試一段時間,以找出異步I/O在我正在處理的Android應用程序中的問題。在Android上等待異步I/O
此應用程序需要從Microsoft Dynamics CRM的一系列表中下載數據。
數據一旦關閉,它必須對數據執行一系列操作以填寫某些表單。
我的問題是,我必須等待下載完成才能開始更新過程。
如果我添加任何形式的等待到我的代碼似乎它無限期地阻止,並從不執行回調。
我嘗試過使用AtomicBooleans,AtomicIntegers和CountDownLatchs沒有成功的方法。
這裏是一個使用AtomicInteger的例子。
final CountDownLatch latch = new CountDownLatch(1);
OrganizationServiceProxy orgService;
orgService = new OrganizationServiceProxy(Constant.ENDPOINT, CRMLogin.getRequestInterceptor());
ColumnSet columnSet = new ColumnSet();
columnSet.AddColumns(AccountEntry.FETCH_COLS);
orgService.Retrieve(AccountEntry.ENTITY, UUID.fromString(accountid), columnSet, new Callback<Entity>() {
@Override
public void success(Entity entity, Response response) {
Account account = new Account();
//Load the existing fields for the account
account.load(index);
String activityid = account.getValue(AccountEntry.ACTIVITY_ID);
String recordid = account.getValue(AccountEntry.RECORD_ID);
String name = account.getValue(AccountEntry.ACCOUNT_NAME);
//Overload the fields for the account
account.load(entity);
//Reset overloaded fields on the account.
account.setValue(AccountEntry.ACTIVITY_ID, activityid);
account.setValue(AccountEntry.RECORD_ID, recordid);
account.setValue(AccountEntry.ACCOUNT_NAME, name);
//overwrite the record in the database.
account.setValue(AccountEntry.SYNCED, "1");
account.update();
Log.d("pullAccount>>>", accountid + " " + "pulled.");
latch.countDown();
}
@Override
public void failure(RetrofitError error) {
Log.d("pullAccount>>>", accountid + " " + error.getMessage());
latch.countDown();
}
});
try{
latch.await(); //THIS BLOCKS FOREVER AND EVER
}
catch (Exception e){
}
值得注意的是CallBack是使用Retrofit實現的。
任何指導將不勝感激。
謝謝。
我不確定這種技術是否可以使用上面提到的回調。 您能否提供代碼來演示您對上述請求響應方案的想法? – earnshae
我會試一試,看看它是如何工作的,併發布我的結果,謝謝代碼。 – earnshae
所以我跑了這個,它仍然存在這樣的問題,即當我等待服務時,我永遠等待。然而,這是一項很好的服務,我編輯了我的代碼以反映這項服務。 – earnshae