Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to a pending request with RoboSpice (Splash > Main activity)?

I have fews questions about RoboSpice (advanced usages).

Note: I use RoboSpice with OrmLite.

My Android application is composed of two main activities: the first one is SplashActivity (start on launch) and the second is MainActivity (lauched 5s after the Splash Screen). I perform 2 requests on splash:

SplashActivity

public class SplashActivity extends BaseActivity {

    private fooRequest fooRequest;
    private barRequest barRequest;
    private exampleRequest exampleRequest;
    private NewsRequest newsRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        fooRequest = new fooRequest();
        barRequest = new barRequest();
        ...
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // Launch MainActivity...
            }
        }, 5000);
    }

    @Override
    protected void onStart() {
        super.onStart();

        getSpiceManager().execute(fooRequest, new Integer(0), DurationInMillis.ONE_DAY, new fooRequestListener());
        getSpiceManager().execute(barRequest, new Integer(1), DurationInMillis.ONE_DAY, new barRequestListener());
    }

    public final class fooRequestListener implements RequestListener<Foo> {
        @Override
        public void onRequestFailure(SpiceException spiceException) {
            Log.w("MyApp", "RoboSpice - Foo request failure");
        }

        @Override
        public void onRequestSuccess(final Foo result) {
        }
    }

    public final class barRequestListener implements RequestListener<Bar> {
        @Override
        public void onRequestFailure(SpiceException spiceException) {
            Log.w("MyApp", "RoboSpice - Bar request failure");
        }

        @Override
        public void onRequestSuccess(final Bar result) {
        }
    }

}

The problem

My application logic is not really reliable: we can not be sure that requests are finished when MainAcitivty is launched. On the MainActivity I query my database with OrmLite for fetch some data and display them. So if request started on SplashActivity are not finished, my View display nothing.

Questions

1) I think that I need to add a listener to my pending requests (if such a request exists). On the RoboSpice Wiki, it said to use spiceManager.addListenerToPendingRequest. I have not managed to put it out, despite my tests. Maybe I'm doing something wrong? Can you give me code example? Resolved (see below)

2) Currently, the user is still waiting 5 seconds (timer Splash) before arriving at the home screen. How to check if data are in cache? With spiceManager.getDataFromCache() (it takes into account the expiration duration?).

3) What is the best retry policy to failed requests (a. at the first launched if database is not again created; b. if the database exists but data are expired)?


Edit

Question #1 resolved (I make a mistake in my original code) - #2 and #3 still relevant. Here's what to do (if it can help someone ...):

public class MainActivity extends BaseActivity {

    private SpiceManager spiceManager = new SpiceManager(CalendarSpiceService.class);
    ...
    @Override
    protected void onStart() {
        super.onStart();

        spiceManager.start(this);
        spiceManager.addListenerIfPending(Foo.class, new Integer(0), new fooRequestListener());
        spiceManager.addListenerIfPending(Bar.class, new Integer(2), new newsRequestListener());
        spiceManager.getFromCache(Foo.class, new Integer(0), DurationInMillis.ONE_DAY, new fooRequestListener());
        spiceManager.getFromCache(Bar.class, new Integer(2), DurationInMillis.ONE_DAY, new newsRequestListener());
    }
    ...
    public final class fooRequestListener implements RequestListener<Foo> {
        @Override
        public void onRequestFailure(SpiceException spiceException) {
            Log.w("MyApp", "RoboSpice - Foo request failure");
        }

        @Override
        public void onRequestSuccess(final Foo result) {
            String test = result.getResult().iterator().next().getTitle();
            Log.w("MyApp", "RoboSpice - Foo request OK ! "+test);
        }
    }

    public final class barRequestListener implements RequestListener<Bar> {
        @Override
        public void onRequestFailure(SpiceException spiceException) {
            Log.w("MyApp", "RoboSpice - Bar request failure");
        }

        @Override
        public void onRequestSuccess(final Bar result) {
            Log.w("MyApp", "RoboSpice - Bar request OK ! "+test);
        }
    }

}

But I don't understand the aim of spiceManager.getFromCache here...

Thanks!

like image 239
Guicara Avatar asked Nov 02 '22 06:11

Guicara


1 Answers

I would make the splash screen last longer. It looks like there is a realy ambiguity on its purpose : displaying a logo or something, or execute requests and display a "please wait message".

In the first case, it would be fine not to execute any request. In the second, it would be fine to wait for them to return.

I really prefer applications that, if they need a splashscreen at all, boot quickly. The splash screen should last less than 1 second. The first screen after it should handle the data querying and please wait message.

But to answer your questions :

2) yes, SpiceManager cache methods use cache expiry in the same way as executeRequest. You can check if there is something in the cache, but you will have to specify what you mean by "valid" data by specifying the expiry limit of the data.

3) I don't see the link with a retry policy and your overall problem. By default, RoboSpice has a retry policy to retry 3 times a failed request. If you can't get the data from your cache then it means there is nothing in it. If your listener's failure hook is invoked, then the network request failed. Both could be the same listener, and your app should have a general mechanism to 1) display that something is wrong, 2) relaunch request if needed after a while (that could be a refresh button/item menu).

I hope I helped, but not so sure.

like image 88
Snicolas Avatar answered Nov 12 '22 14:11

Snicolas