How do I trigger an update of the data in the view?


#1

I’m having some problems with updating data in my view. I have a service that returns data and I can see in the console it is fetching the newest post.

However, in my Ionic app the newest post is not displayed. I’m calling the service in ionViewDidEnter and I have even tried forcing the view to update using the NgZone in Angular. No solution has worked so far.
I have also tried to use applicationRef.tick() but has not helped either.

It works fine when in the browser when I disable cache. Then I can see the newest post. But it does not work on my device as no new data is displayed after it has cached the first data.

Do you have any idea how to solve this?

This is my typescript file and you can see my Ionic info below.

  /**
   * @description this is where the data loads from the service. This event always fires. It is a way of stopping caching in the app.
   */
  ionViewDidEnter() {
    this.zone.run(() => {
      this.loadItems();
    });
  }

  /**
   * @description loads all items
   */
  loadItems() {
    
    // Create the loader
    let loader = this.loadingController.create({
      content: "Getting data.."
    });
    loader.present();

    // Get the most recent 100 posts
    this.itemApi.getItems(100).then(data => {
      loader.dismiss();
      this.posts = data
      this.initializeItems();
    });

  }

  /**
   * @description set the new data equal to the items returned.
   */
  initializeItems() {
    // The data is reset to all items
    this.zone.run(() => {
      this.items = this.posts;
    });
  }

My Ionic info:

cli packages: (/Users/kristofferandreasen/Desktop/svampemad/node_modules)

    @ionic/cli-utils  : 1.19.2
    ionic (Ionic CLI) : 3.20.0

global packages:

    cordova (Cordova CLI) : 8.0.0

local packages:

    @ionic/app-scripts : 1.3.7
    Cordova Platforms  : android 6.4.0 ios 4.5.4
    Ionic Framework    : ionic-angular 3.1.1

System:

    Node : v8.10.0
    npm  : 5.6.0
    OS   : macOS High Sierra

Environment Variables:

    ANDROID_HOME : not set