仅从AngularFire列表中获取新项目
我正在使用Firebase RTDB列表实现一个聊天应用程序。下面是一个简化的数据服务实现:仅从AngularFire列表中获取新项目,angular,firebase,firebase-realtime-database,angularfire2,Angular,Firebase,Firebase Realtime Database,Angularfire2,我正在使用Firebase RTDB列表实现一个聊天应用程序。下面是一个简化的数据服务实现: public monitorConversation(conversationId): Observable<any> { return this.afDB.list(`/conversations/${conversationId}/messages`).stateChanges(); } publicmonitorconversation(conversationId):可观察{
public monitorConversation(conversationId): Observable<any> {
return this.afDB.list(`/conversations/${conversationId}/messages`).stateChanges();
}
publicmonitorconversation(conversationId):可观察{
返回此.afDB.list(`/conversations/${conversationId}/messages`).stateChanges();
}
然后我从一个组件(页面)调用它,如:
msgList:可见;
this.msgList=this.dp.monitorConversation(conversationId);
this.msgList.subscribe(消息=>{
log('new msg:',message.payload.val());
返回消息.payload.val();
});
});
期望的结果是,每次在对话中添加新消息时,我都会收到新消息。现在发生的是,我收到了所有的信息。我担心的是,随着消息列表的增长,UI性能将开始下降,因为我必须重新显示列表,以及它发送不必要流量所消耗的带宽,因为我假设整个列表也正在从Firebase检索
是否有办法只接收更改或新项目?项目列表中有$keys,所以我知道哪个项目是哪个 当然,有可能:
Firebase实时数据库
this.rtdb
.list('path', ref => ref.orderByChild('timestamp').startAt(Date.now()))
.stateChanges();
this.afs
.list('path', ref => ref.where('timestamp', '>', new Date()).orderBy('timestamp'))
.stateChanges();
Firebase Firestore
this.rtdb
.list('path', ref => ref.orderByChild('timestamp').startAt(Date.now()))
.stateChanges();
this.afs
.list('path', ref => ref.where('timestamp', '>', new Date()).orderBy('timestamp'))
.stateChanges();
您还可以使用limitToLast
或limit
获取有关angularfire2官方的最新项目
非常感谢。我想我已经在AngularFire2中发现了一个我正试图复制的bug。如果一个列表(不是查询)上有多个订阅,出于某种原因,对列表的更改将返回所有记录。一旦我从另一个观看同一个列表的观察者那里退订,它就表示工作正常。