Skip to content
This repository has been archived by the owner on Jan 14, 2018. It is now read-only.

getDataFromCache(...) not working #30

Closed
papo2608 opened this issue Jan 17, 2013 · 6 comments
Closed

getDataFromCache(...) not working #30

papo2608 opened this issue Jan 17, 2013 · 6 comments

Comments

@papo2608
Copy link

I tried to use getDataFromCache(final Class clazz, final String cacheKey) in onResume of an activity but the app ends in an ANR error.

The SpiceManger was started in onStart. I figured out that the service is never connected in this case (onServiceConnected isn't called) and so waitForServiceToBeBound ends in an infinite loop.

Can you please tell me whats the right time/place to call getDataFromCache?

@rciovati
Copy link
Contributor

Could you post a simple project that reproduces this?

@papo2608
Copy link
Author

You can reproduce this issue with:

  1. checkout robospice-sample-spring-android
  2. I'm using Gson instead of Jackson
  3. put following lines of code in your MainActivity class:
    @Override
    protected void onResume() {
        super.onResume();
        ListTweets tweets;
        try {
            Future<ListTweets> task = contentManager.getDataFromCache(ListTweets.class, "tweets.android");
            tweets = task.get();
        } catch (CacheLoadingException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

4 search with the query "android"
5 you get an ANR error

@stephanenicolas
Copy link
Owner

Was you spiceManager started before reaching this line ?

Also, please consider the asynchronous nature of a call to
getDataFromCache. When you call this method, it will return a future as the
spiceManager must bind to the spiceService and, when bound, ask for the
cache content.

When you call the method get() of that future, it will block until the
getDataFromCache actually completes. This operation should not be called on
the main thread but inside an AsyncTask. Otherwise, get will block your UI
thread and create an ANR. There is no workaround for it.

Stéphane

2013/1/19 papo2608 notifications@github.com

You can reproduce this issue with:

  1. checkout robospice-sample-spring-android
  2. I'm using Gson instead of Jackson
  3. put following lines of code in your MainActivity class:
@Override
protected void onResume() {
    super.onResume();
    ListTweets tweets;
    try {
        Future<ListTweets> task = contentManager.getDataFromCache(ListTweets.class, "tweets.android");
        tweets = task.get();
    } catch (CacheLoadingException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}

4 search with the query "android"
5 you get an ANR error


Reply to this email directly or view it on GitHubhttps://github.com//issues/30#issuecomment-12455292.

Stéphane NICOLAS,
OCTO Technology
Développeur & Consultant Android / Java
..........................................................
50, Avenue des Champs-Elysées
75008 Paris
+33 (0)6.26.32.34.09
www.octo.com - blog.octo.com
www.usievents.com
...........................................................

@rciovati
Copy link
Contributor

I was writing what @stephanenicolas said.

BTW, you can use the getFromCachemethod that is async. ;)
We use it in the same sample: http://bit.ly/WrhFNn

@stephanenicolas
Copy link
Owner

Right. Thx Ricardo. ;)

2013/1/19 Riccardo Ciovati notifications@github.com

I was writing what stephanenicolas said.

BTW, you can use the getFromCachemethod wich is async. ;)


Reply to this email directly or view it on GitHubhttps://github.com//issues/30#issuecomment-12455417.

Stéphane NICOLAS,
OCTO Technology
Développeur & Consultant Android / Java
..........................................................
50, Avenue des Champs-Elysées
75008 Paris
+33 (0)6.26.32.34.09
www.octo.com - blog.octo.com
www.usievents.com
...........................................................

@papo2608
Copy link
Author

OK, so there is no possibility to get cached data synchronously?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants