Caching a JSON query in IONIC 3

Asked

Viewed 198 times

0

Hello, I have an App that consumes JSON and lists the data, so far so good. My difficulty is in keeping this data for offline access. I want to consult after you’re offline.

P.S.: I’ve been testing with Service Worker, it works perfectly but in the browser, when I put it in the app, it doesn’t work, it doesn’t keep the cache.

I would like some links or some success case in this.

From now on Thank you!

  • Why not save to localStorage?

  • I’m thinking seriously about using, but I want to see something cached, my priority is to use cache. Because the Application only consumes via json, it receives the data and stores in the cache. for when there is no network it searches in the cache.

  • Well, if you think so. In my apps with Ionic I use without any fear when it is to store small information in JSON format. In which I needed to save more information as photo blob for offline use I used Cordova.

1 answer

0


Use the The Ionic Storage:

...

import { Storage } from '@ionic/storage';

@NgModule({

   ...

   providers: [
     Storage
   ]
 })


export class AppModule {}

...


import { Storage } from '@ionic/storage';

export class MyApp {
    constructor(storage: Storage) {

    // set        key   value
    storage.set('myKey', 'myVal');

    // get value 
    storage.get('myKey').then((val) => {

      //CASO HAJA VALOR, NAO REALIZAR CONSULTA EXTERNA
      if(val)
      //...

    })
 }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.