2016-03-11 43 views
0

我一直在嘗試一段時間,以找出異步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實現的。

任何指導將不勝感激。

謝謝。

回答

0

看看AsyncTask它會以Android的優化方式處理你想要的東西。這裏有例如使用

http://developer.android.com/reference/android/os/AsyncTask.html

編輯:

我還挺扔了一起,讓我知道,如果它是你所期望的

public class AsyncOrganizationService extends AsyncTask<Void, Void, Entity> { 
@Override 
protected Entity doInBackground(Void... params) { 
    final CountDownLatch blocker = new CountDownLatch(1); 
    OrganizationServiceProxy orgService; 
    orgService = new OrganizationServiceProxy(Constant.ENDPOINT, CRMLogin.getRequestInterceptor()); 
    ColumnSet columnSet = new ColumnSet(); 
    columnSet.AddColumns(AccountEntry.FETCH_COLS); 
    final SettableFuture<Entity> result = SettableFuture.create(); 
    orgService.Retrieve(AccountEntry.ENTITY, UUID.fromString(accountid), columnSet, new SortedList.Callback<Entity>() { 
     @Override 
     public void success(Entity entity, HttpHelper.Response response) { 
      result.set(entity); 
      blocker.countDown(); 
     } 
    }); 
    try { 
     blocker.await(); 
     return result.get(); 
    } catch (InterruptedException | ExecutionException e) { 
     throw new RuntimeException(e); 
    } 
} 

@Override 
protected void onPostExecute(Entity entity) { 
    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."); 
} 

進出口使用番石榴的SettableFuture類(http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/util/concurrent/SettableFuture.html )。番石榴是一個了不起的圖書館 - 如果你不使用它,你應該考慮這樣做。否則,你可以真的很快鞭打一些東西

+0

我不確定這種技術是否可以使用上面提到的回調。 您能否提供代碼來演示您對上述請求響應方案的想法? – earnshae

+0

我會試一試,看看它是如何工作的,併發布我的結果,謝謝代碼。 – earnshae

+0

所以我跑了這個,它仍然存在這樣的問題,即當我等待服務時,我永遠等待。然而,這是一項很好的服務,我編輯了我的代碼以反映這項服務。 – earnshae