2014-09-10 125 views
2

我需要實現非常流行的應用行爲模板 - 爲用戶提供嘗試重試失敗請求的機會。現在我用SpiceServiceListener捕獲失敗的請求,並顯示用戶可以按「重試」按鈕的對話框。不幸的是,使用與SpiceManager.execute()相同的CachedSpiceRequest對象不會給出所需的行爲,因爲如果請求不成功,RS會從mapRequestToLaunchToRequestListener中刪除所有請求偵聽器。所以請求可以正常工作,但不會將任何信息返回給我的活動。Robospice。按需重試失敗請求

有沒有簡單的方法(沒有修改庫的代碼)來實現這個?

+0

看起來像刪除聽衆不是唯一的問題。 'SpiceRequest'有許多「狀態變量」('isCanceled','future','progress'),所以如果重新使用它而沒有對這些變量進行適當的重新初始化可能會導致意想不到的結果。 – Deinlandel 2014-09-11 11:59:50

+0

如果您找到解決方案,請分享,如果OAuth令牌過期,我需要重試。 – BornToCode 2014-10-02 06:17:29

+0

@BornToCode請檢查我的答案 – Divers 2014-10-03 07:50:57

回答

0

不幸的是,看起來像這樣的情況沒有抽象的解決方案,所以我必須在每個請求中添加這樣的代碼。

getSpiceManager().execute(r, new RequestListener<CountProfiles>() { 
     @Override 
     public void onRequestFailure(SpiceException spiceException) { 
      if (act.getSupportFragmentManager().findFragmentByTag("network_problem") == null) { 
       NetworkProblemDialogFragm.newInstance(r, this).show(act.getSupportFragmentManager(), "network_problem"); 
      } else { 
       ((NetworkProblemDialogFragm) act.getSupportFragmentManager().findFragmentByTag("network_problem")).setSpiceRequest(r); 
       ((NetworkProblemDialogFragm) act.getSupportFragmentManager().findFragmentByTag("network_problem")).setRequestListener(this); 
      } 
     } 

     @Override 
     public void onRequestSuccess(CountProfiles countProfiles) { 
     } 
    }); 

NetworkProblemDialogFragm是重試按鈕DialogFragment,在點擊這個按鈕,我重新執行失敗的請求,利用給定的RequestListener。

不是很漂亮的解決方案,但看起來像沒有更好的解決方案。