0

What is the best way to make an http request and when there is a new entry add it to the local storage ?

In my case, I have a JSON API and I would like to retrieve my articles and store them whenever there is a new article.

I looked on the internet but did not find exactly what I wanted.

Thank you very much in advance

Valentin Harrang
  • 1,081
  • 2
  • 17
  • 34
  • No one to help me ? – Valentin Harrang Jan 28 '18 at 22:28
  • your question is abit vague, if you want to reload whenever there is a new article you have 2 choice; either have a timer that periodically all the service that retrieve the article or use websocket to create a real-time connection with the back end.You should not be abusing local-storage to store thing like this. – Asura Jan 28 '18 at 22:49
  • In fact, I'd like to make an http request and keep my items in my phone's memory if the user no longer has an internet connection and when his internet connection is restored then I would make a new http request to update my local storage – Valentin Harrang Jan 28 '18 at 22:52
  • ah what you want is called an offline web application/app; plenty of resource on the web and too generic to cover here. – Asura Jan 28 '18 at 23:10
  • I want to make an HTTP request on my JSON API when the user is connected to the internet (which I think is the normal operation of a news application) and if the user no longer has an internet connection then the localstorage takes over by displaying the data that's inside. If you had to create a news application by retrieving your articles via a JSON API, how would you keep them in mind if the user no longer has a connection? – Valentin Harrang Jan 28 '18 at 23:26
  • I would keep the data returned by JSON API in the either the app memory via variable(which would be gone once the app or phone is turned off) or save the returned data on the HardDrive in a text file or something and have the program read it when its in offline mode. – Asura Jan 28 '18 at 23:34
  • That's what I would like to do, but how would you do that? You would make an http request, create an article object and convert it into a JSON object, store it in the Ionic "storage" ? – Valentin Harrang Jan 28 '18 at 23:42
  • yes, storage in ionic is explained here with simple example https://ionicframework.com/docs/storage/ – Asura Jan 28 '18 at 23:59
  • Yes but before doing that I return a Observable from my function. how can i convert it to a json object? – Valentin Harrang Jan 29 '18 at 00:05
  • plenty of way online to convert an object to JSON, you also dont have to store the JSON, you can just store the IArticleCard[] returned from the subscription. – Asura Jan 29 '18 at 00:23
  • Like that ? this.observable$.subscribe(articles => { storage.set('articles', JSON.stringify(articles)); }); – Valentin Harrang Jan 29 '18 at 08:49

1 Answers1

0

I believe you'll want an Interval Observable.

Previous post on this here. Angular2 http at an interval

Blog post on same https://edqv.ist/angular-2-using-observables-at-an-interval/

Streamweaver
  • 375
  • 2
  • 8
  • Sorry, I was not clear. I would like to make an http request and save my data in the phone's memory if the user cuts his internet connection. And when it has a connection again, the http request will resume to overwrite what is currently in the local storage. This is a news app, it is necessary that the articles remain displayed offline – Valentin Harrang Jan 28 '18 at 23:00
  • Ah, you probably want to use something like the localstorage api. Pull the results and then localstorage.getItem and localstrage.setItem should do. The particular logic of when to revert to it will depend on how you want the network failover to work. https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage – Streamweaver Jan 29 '18 at 00:55