是否可以在Ionic2中实时更新临时数组?

时间:2017-10-07 07:50:28

标签: javascript ionic2 real-time

我不确定我的问题是否可行,但我想问是否有办法让一个实时更新的arraylist

例如,我使用" setInterval"更新我的临时arraylist,如:

public usersList: FirebaseListObservable < any[] > ;
public chatsList: FirebaseListObservable < any[] > ;
public tempList = [];

public refreshIntervalId;

constructor(...) {
    this.usersList = this.afd.list('/users/');
    this.chatsList = this.afd.list('/chats/');
    this.refreshIntervalId = setInterval(() => {
        this.joinObjects();
    }, 250);
}

joinObjects() {
    let TempListX = [];
    this.chatsList.take(1).subscribe(data1 => {
        this.usersList.take(1).subscribe(data2 => {
            TempListX = data1.slice(0);
            for (let i = 0; i < data1.length; i++) {
                for (let j = 0; j < data2.length; j++) {
                    if (data1[i].user_id == data2[j].$key) {
                        TempListX[i].fname = data2[j].fname;
                        TempListX[i].lname = data2[j].lname;
                        TempListX[i].uemail = data2[j].email;
                        TempListX[i].uid = data2[j].$key;
                    }
                }
            }
            if (JSON.stringify(TempListX) === JSON.stringify(this.tempList)) {}
            else {
                this.tempList = TempListX.slice();
            }
        })
    })
}

那么有什么方法比#34; setInterval&#34;更好。 ?

1 个答案:

答案 0 :(得分:0)

如果更新基于事件,那么您可以使用事件(订阅和发布)。见文档。

https://ionicframework.com/docs/api/util/Events/