Store icon indicating copy to clipboard operation
Store copied to clipboard

Get data always hits the network without persister

Open mapacheverdugo opened this issue 7 years ago • 5 comments

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");
                        }
                    });

mapacheverdugo avatar Aug 18 '18 20:08 mapacheverdugo

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?

digitalbuddha avatar Aug 18 '18 23:08 digitalbuddha

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?

mapacheverdugo avatar Aug 19 '18 01:08 mapacheverdugo

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

digitalbuddha avatar Aug 19 '18 02:08 digitalbuddha

https://github.com/mapacheverdugo/store-test

mapacheverdugo avatar Aug 20 '18 15:08 mapacheverdugo

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

digitalbuddha avatar Dec 31 '18 17:12 digitalbuddha