stephanenicolas / robospice

Repo of the Open Source Android library : RoboSpice. RoboSpice is a modular android library that makes writing asynchronous long running tasks easy. It is specialized in network requests, supports caching and offers REST requests out-of-the box using extension modules.
Apache License 2.0
2.95k stars 545 forks source link

Asynchronous requests crashing #374

Closed prettyvoid closed 10 years ago

prettyvoid commented 10 years ago

Hello, I'm using a view pager and I have a SpiceManager on the context level (activity). From two different tabs in the pager I do 2 calls at the same time:

((Main)getActivity()).executeGetRequest(listener, WebRequestsHandler.WebCalls.CONTACTS);
((Main)getActivity()).executeGetRequest(listener, WebRequestsHandler.WebCalls.ACTIVE_FILES);

My Main activity looks like this:

    private SpiceManager spiceManager = new SpiceManager(ThreadedSpiceService.class);

    //Many defaults in this method
    public void executeGetRequest(RequestListener listener, String url) {
        WebRequestsHandler webRequestsHandler = new WebRequestsHandler(null);
        webRequestsHandler.setUrl(url);
        spiceManager.execute(webRequestsHandler,"", DurationInMillis.ALWAYS_EXPIRED, listener);
    }

    @Override
    protected void onStart() {
        spiceManager.start(this);
        super.onStart();
    }

    @Override
    protected void onStop() {
        spiceManager.shouldStop();
        super.onStop();
    }

ThreadedSpiceService extends JacksonGoogleHttpClientSpiceService and returns 4 threads.

WebRequestsHandler extends GoogleHttpClientSpiceRequest

This makes my application crash on launch, if I comment one of the 2 requests above, the application runs fine.

Any idea what's wrong with my design? Why it's crashing?

Here is the log: http://i.imgur.com/zdFG8Dn.png

Thanks guys,

prettyvoid commented 10 years ago

I managed to pinpoint the problem, it turned out to be in this line spiceManager.execute(webRequestsHandler,"", DurationInMillis.ALWAYS_EXPIRED, listener);

If I supply a unique requestCacheKey for each request (instead of an empty string), the crash no longer occurs.

Because I'm using no cache, I switched to spiceManager.execute(webRequestsHandler, listener);

It's working fine now.

I think all of this is happening because I designed my SpiceRequest to always return a string, I do the casting in the receiving listener instead of inside the SpiceRequest itself.

Sorry for the troubles