nytimes / Store

Android Library for Async Data Loading and Caching

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Get data always hits the network without persister

mapacheverdugo opened this issue · 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");
                        }
                    });

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?

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?

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

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