Search code examples
androidrequestrobospice

Robospice. Retry failed request on demand


I need to implement quite popular template of app behaviour - give opportunity to user to retry failed requests. Right now I catch failed request with SpiceServiceListener, and shows dialog where user can press "Retry" button. Unfortunately, using the same CachedSpiceRequest object with SpiceManager.execute() don't give desired behaviour, because RS removing all request listeners from mapRequestToLaunchToRequestListener if request wasn't successful. So request can work fine, but it will not return any information to my Activity.

Is there easy way (without modifying code of library) to implement this?


Solution

  • Unfortunately looks like there are no abstract solution for situation like this, so I had to add code like this in every request.

        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 is a DialogFragment with Retry button, on click on this button I re execute failed request, using given RequestListener.

    Not very beautiful solution, but looks like there no better one.