Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Get data always hits the network without persister #364

Open
mapacheverdugo opened this issue Aug 18, 2018 · 5 comments
Open

Get data always hits the network without persister #364

mapacheverdugo opened this issue Aug 18, 2018 · 5 comments

Comments

@mapacheverdugo
Copy link

First of all, I'm still using Java and I'm really noob with RxJava.

I'm trying to retrieve data from a localhosted API, but when I call get() it always hits the network, even when I've previously called the method.

I've been trying many examples, but the problem persists and I don't know what I'm doing wrong. If you could give me an example of the simplest way to make it work, I would appreciate it very much. Here is my current code:

Gson gson = new GsonBuilder()
                .setFieldNamingPolicy(FieldNamingPolicy.IDENTITY)
                .create();

ApiUtem apiUtem = new Retrofit.Builder()
                .baseUrl(ApiUtem.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
                .create(ApiUtem.class);

Store<Student, BarCode> store = StoreBuilder.<Student>barcode()
                .fetcher(barCode -> apiUtem.getStudent(barCode.getKey(), mToken)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread()))
                .memoryPolicy(
                        MemoryPolicy
                                .builder()
                                .setExpireAfterWrite(10)
                                .setExpireAfterTimeUnit(TimeUnit.MINUTES)
                                .build()
                )
                .open();
BarCode barcode = new BarCode(Student.class.getSimpleName(), "19649846");
store.getWithResult(barcode)
                    .subscribe(studentResult -> {
                        if (studentResult.isFromCache()) {
                            Log.d(TAG, "Cache");
                        } else {
                            Log.d(TAG, "Network");
                        }
                    });
@digitalbuddha
Copy link
Contributor

Hi sad to hear you are having issue. We have a test that covers the above use case https://github.com/NYTimes/Store/blob/46e0b7fd5aeaaffe66d76ab1c88bb9e4c6872bd3/store/src/test/java/com/nytimes/android/external/store3/StoreTest.java#L73

One reason that the memory cache would be skipped is if the equality check fails. Is it possible that you are not implementing equals/hashCode in your data class?

@mapacheverdugo
Copy link
Author

I had not implemented equals/hashCode as you said, now that I did it, it still doesn't work. I've even changed the API endpoint to receive a simple String, but the problem persist. Any other ideas?

@digitalbuddha
Copy link
Contributor

Create a sample project and I'd be happy to take a look

@mapacheverdugo
Copy link
Author

@mapacheverdugo mapacheverdugo changed the title Get data always hits the network Get data always hits the network without a persister Aug 27, 2018
@mapacheverdugo mapacheverdugo changed the title Get data always hits the network without a persister Get data always hits the network without persister Aug 27, 2018
@digitalbuddha
Copy link
Contributor

so sorry for not replying for this long, your link is not active I can take a look if you update

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

2 participants