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