1

I create a news application by getting data from my JSON API and I would simply like to store my data in Ionic's storage and display what's in storage when the user no longer has an internet connection.

Currently, I retrieve my data with the HttpClient and return an Observable .

I can store them in the storage and display it. But I'm not sure that my life cycle is working properly and I don't know if what I did is good.

ArticleService :

import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import 'rxjs/add/operator/map';
import {Platform} from "ionic-angular";
import {ArticleCard} from "../classes/ArticleCard";
import {Observable} from "rxjs/Observable";
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import {IArticleCard} from "../interfaces/IArticleCard";

@Injectable()
export class ArticlesService {

  urlArticlesCards: string = "https://mylink";

  constructor(private http: HttpClient, public platform: Platform) {

  }

  getAlaUneCards(): Observable<IArticleCard[]> {
    return this.http.get<any>(this.urlArticlesCards)
      .map(res => {
        return res.map(article => {
          return new ArticleCard(
            article.id,
            article.image,
            article.titre,
          );
        });
      });
  }
}

Interface :

export interface IArticleCard {
  results: {
    id: number;
    image: string;
    titre: string;
  }[]
}

ArticleCard :

export class ArticleCard {
  id: number;
  image: string;
  titre: string;

  constructor(id: number, image: string, titre: string) {
    this.id = id;
    this.image = image;
    this.titre = titre;
  }
}

Home :

  observable$: Observable<IArticleCard[]>;
  tabCards: Array<Object>= [];

  constructor(public navCtrl: NavController, private articleService: ArticlesService, private storage: Storage) {}

  ionViewWillEnter() {

    this.observable$ = this.articleService.getAlaUneCards();

    this.observable$.subscribe(cards => {

      if(cards){
        this.storage.set('alaune_cards', cards);
      }

      this.storage.get('alaune_cards').then((card) => {
        this.tabCards = card;
      })

    });

  }
0

1 Answer 1

1

I can think of some simple improvements:

1) You don't need to re-grab what you just stored in subscribe method. You can just set this.tabCards = card; inside the if (cards) { branch.

2) I'd only suggest storing a finite amount of data, perhaps the last 50 articles? Anything past that can be grabbed historically from the GET endpoint.

3) If your goal is to display many articles in a list, I highly suggest implementing infinite scroll https://ionicframework.com/docs/api/components/infinite-scroll/InfiniteScroll/ . See if you can make your urlArticlesCards GET endpoint pageable. This will make your app perform better overall.

Sign up to request clarification or add additional context in comments.

9 Comments

You're right, I am currently collecting 137 cards via my JSON API, if one day I have 1000 cards the application will be very heavy. So I have to recover the last 20 for example and when the user scroll, the virtual scroll will load the next 20?
Sorry! When talking about dynamically loading data, I meant to say InfiniteScroll: ionicframework.com/docs/api/components/infinite-scroll/…
Thank you for your help but it not working :/ the infinitescroll not displayed the 10 next articles after the chargement.. pastebin.com/XhyR98HG
Try to figure out where it's failing using some console log statements. See if doInfinite() is getting called.
My console.log("=== doInfinite subscribe ==="); is displayed in the console and the function to get my articles is called
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.