P
P
pcdesign2019-03-26 12:58:59
React Native
pcdesign, 2019-03-26 12:58:59

How not to download the same data from the API if there were no changes?

Here is the code that receives the data via the API:

async componentDidMount() {
    var URL = 'https://example.com/api/get/data/'; 
    await fetch(URL)
      .then((response) => response.text())
      .then(async (responseText) => { 
        await AsyncStorage.setItem('db_qs', responseText);        
      })
      .catch((error) => {
        console.warn(error);
      });
}

Everything works, but the problem is that this code drags data every time, even if they have not been changed.
I would not want the application to needlessly download data over the Internet.
How can this be bypassed?
I tried to check through headers. Just checking Content-Length and Date but something is not working .
And how to cache this data so that it is stored and not updated?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2019-03-26
@rockon404

How not to download the same data from the API if there were no changes?

Request only new and changed data.

A
Andrew, 2019-05-01
@undefined_title

There is a type of web sockets connection, in which the server itself can send you data when they have changed, I don’t know how suitable this option is in your case, but you won’t be able to find out from the front when the data has changed on the server, only on the server it’s clear and it should notify the client part about it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question