2016-12-16 111 views
0

我試圖在我的Ionic 2應用程序中加載一些消息,並將它們存儲在SqLite數據庫中以避免多次下載它們。Ionic 2 * ng對諾言不承諾(承諾)

所以基本上我問我的Sqlite最後一個ID保存,然後問服務器所有消息比該ID更新,然後用保存的消息填充董事會,直到我達到20消息(顯然與一個單一的電話)。

所以技術部分:我把所有這些消息放在一個數組中,應該用* ngFor在App中顯示。當我在PC上運行應用程序時,它顯示正確(所以邏輯是正確的),但是當我在手機上運行應用程序時,就好像* ngFor沒有刷新並且消息未顯示。

不總是,順便說一句。如果我卸載頁面,我看到了一會兒的消息(該數組最終顯示在* ngFor),如果我再次加載它顯示它們(不是因爲現在它們被保存,而是因爲它們已經在某個緩存中認爲)。

答案是:我缺少的* ngFor有限制嗎?我怎樣才能讓消息一直顯示?我已經將消息限制從100個降低到了20個,但它沒有解決問題。

下面是代碼,與三個承諾:

的getMessages(){ this.dbService.getLastMessageId(),然後((數據)=> {

let lastId = 0;  
    if (data.res.rows.length > 0) 
    lastId= data.res.rows.item(0).Id; 


    this.http.get(URL+lastId) 
     .subscribe(data => { 

     var response = data.json(); 
     let newMessagesCount = response[0].count; 
     for (let i =1; i<=newMessagesCount; i++) 
     this.messages.push(response[i]); 

     if (newMessagesCount <20) 
     this.dbService.getOldChatMessages(20-newMessagesCount).then((data) => { 

     if (data.res.rows.length > 0){ 
     let rowsCount = data.res.rows.length; 
     for (let i=0; i<rowsCount;i++){ 
      let newElement = { 
      Name: data.res.rows.item(i).Name, 
      Date: data.res.rows.item(i).mexDate, 
      Text: data.res.rows.item(i).mexText, 
      Id: data.res.rows.item(i).Id, 
      New: 0 
      }; 
      this.messages.push(newElement); 

      }     
     } 
    //HERE I EXPECT THE ARRAY TO BE READY 
     if(newMessagesCount>0){ 
     this.dbService.storeNewChatMessages(response);      
     } 
     }); 
    }); 
    }); 

}

回答

0

嘗試把你的循環在這樣的區域:

import { NgZone } from '@angular/core'; 

export class MyPage { 
    zone: NgZone; 

    constructor() { 
    this.zone = new NgZone({enableLongStackTrace: false}); 
    } 

    getMessages() { 

    this.zone.run(() => { 
     for (let i =1; i<=newMessagesCount; i++) { 
     this.messages.push(response[i]); 
     } 
    }); 

    this.zone.run(() => { 
     for (let i = 0; i < rowsCount; i++) { 
     let newElement = { 
      Name: data.res.rows.item(i).Name, 
      Date: data.res.rows.item(i).mexDate, 
      Text: data.res.rows.item(i).mexText, 
      Id: data.res.rows.item(i).Id, 
      New: 0 
     }; 

     this.messages.push(newElement); 
     } 
    }); 
    } 
} 
+0

謝謝你的答案,但它doesn沒有工作。隨着你的建議,現在在個人電腦上的工作就像以前一樣,但在我的Android設備上,它不再進入該頁面(當應用程序進入該頁面時該方法被調用一次) –

+0

您使用什麼版本的Ionic ?在設備上運行並打開頁面時是否有任何js錯誤? –

+0

Ionic 2 rc.3,我無法看到設備上的js錯誤 –