0

I am using AWS AppSync for Android, and I want to display a list of Persons into a RecyclerView. I am fetching paginated data with the AppSyncResponseFetchers.CACHE_AND_NETWORK flag. If data is present in the cache, onResponse is called twice, causing data duplication in the RecyclerView.

What is the right way to combine data returned by these 2 calls?

private void loadNextPersons() {
    AWSAppSyncClient client = AppSyncClientFactory.getAppSyncClient(AppSyncAuthMode.KEY);
    client.query(AllPersonsByCityQuery.builder()
             .city(cityName)
             .limit(10)
             .nextToken(nextToken)
             .build())
           .responseFetcher(AppSyncResponseFetchers.CACHE_AND_NETWORK).enqueue(loadCallback);
}

private GraphQLCall.Callback<AllPersonsByCityQuery.Data> loadCallback= new GraphQLCall.Callback<AllPersonsByCityQuery.Data>() {
    @Override
    public void onResponse(@Nonnull Response<AllPersonsByCityQuery.Data> response) {
        // Called twice if we have data in the cache and network connection
        runOnUiThread(() -> {
            adapter.addData(response.data().allPersonsByCity().persons());
        }
    }
}
Jameson
  • 6,400
  • 6
  • 32
  • 53
irkForce
  • 352
  • 2
  • 12
  • In the responseFetcher() method, why are u giving it Network and cache params? – Jason Jun 11 '20 at 16:24
  • @Jason, because I want to get fast results from cache first even if they are stale and then update them with the latest data from server – irkForce Jun 11 '20 at 23:03
  • Then you just separate the methods. Do the cache call before the Network call – Jason Jun 12 '20 at 10:15
  • .... don't `>>>add<< – xadm Jun 12 '20 at 10:28
  • @xadm, yeah I found out that ```response``` contains fromCache() which returns true or false. It means that I should write bulky logic based on fromCache() and another boolean variable in onResponse(). Is there a neater way? – irkForce Jun 12 '20 at 12:48
  • what it can change if you still will be **add**ing results?? use other method of useining results, universal for both responses/calls – xadm Jun 12 '20 at 12:58

0 Answers0