Retrieving and loading data from Storage


#1

Hi guys, I am building an application and trying to retrieve data from server and display it separately. for this I am using Storage, in app.component.ts I have called the server.ts and stored it to a shared local service:

app.component.ts:

 constructor(platform: Platform, public jobsData: JobsData, public storage: Storage, public local: Local) {
platform.ready().then(() => {
  // Okay, so the platform is ready and our plugins are available.
  // Here you can do any higher level native things you might need.
  StatusBar.styleDefault();
  Splashscreen.hide();

  jobsData.load().subscribe(res => {
  local.localData(res);
});
});

This is the local service:

@Injectable()
 export class Local {

   constructor(public storage: Storage) { }
    localData(data){
     this.setLocalData(data);
    }

    setLocalData(data){
      this.storage.set('data', JSON.stringify(data));
    };

   getLocalData(){
     return this.storage.get('data').then((value) => {
     return JSON.parse(value);
     });
    };

and Im displaying the data on a specific page in the constructor as follows:

local.getLocalData().then((res) => {
    this.jobs = res;
    console.log(this.jobs)
  });

On web console it retrieves and displays the data instantly, However, when I build the .apk file and install it on Android and open it for the first time… it does not load anything, until I exit the program and open it again!!!

I don’t know why it does this… Or perhaps if there is a better way to do this!!