When the page is loaded, the data is correctly fetched, and shown, then when leaving the page the data is fetched again and doubled onto the current array. If I have Item: A,B,C then I will have A,B,CA,B,C.
Right now I have the component putting a simple if to check if I need to fetch the data again or not. However, it appears that it is bypassed. I have looked at this and this as well as this.
//home.ts
export class Home implements OnInit, OnDestroy{
  conversations: Array<any>; 
async ngOnInit() {
  this._chatInit();
}
private async _chatInit() {
  this.dataService.loadConversations(); //getter for local storage
  const data = this.messageStorage.hasChats();
  if (data.length) {
    //there is data already loaded
    this.conversations = data;
  } else {
    //there is an empty array, subscribe to it.
    this.messageStorage
    .getChatList$()
    .subscribe(conversation => {
      console.log('Conversation', conversation)
      this.conversations = conversation;
    });
  }
}
//dataService
export class DataService {
  //the object where it is all stored
  private conversations: any = {
    peerToPeer: {},
    groups: {},
  };
  private listOfChats: Array<any> = new Array<any>();
  private bsListOfChats$: BehaviorSubject<any> = new BehaviorSubject<any>(this.listOfChats); 
  public loadConversations() {
    this.storage.get('conversations').then(chat=> {
      this.conversations = chat;
      this.formatChats(this.conversations);//converts the Object to an Array so *ngFor directive can be used
   });
  }
  public hasChats() {
    return this.listOfChats;
  }
  public getChatList$() {
    return this.bsListOfChats$;
  }
}
To clarify, what I want to happen is to load n chats once. When I leave the page and return I want the same n to load, no more, no less.
thanks guys for any help!
 
     
     
    