Hi, your current structure is not very healthy. When there are too many messages, take all messages as they are will both tire the device and increase costs. Below is an example that I think is more accurate. When you apply this structure, your problem will be solved.
init-messages.ts
// The part to run when the chat page is first opened. (ionViewDidEnter or ngOnInit)
public async initMessages(): Promise<Message[]> {
const result = await this.fireStore.collection('messages').ref
.orderBy('date', 'desc')
.limit(20).get();
return result.docs.map((value) => {
return {
id: value.id,
...value.data() as Message
This file has been truncated. show original
listener.ts
// The function that will notify us when a new message arrives, has been deleted or edited.
public messageListener(type: firebase.firestore.DocumentChangeType) {
return this.fireStore.collection('messages', (query) =>
query.orderBy('date')
).stateChanges([type]);
}
// Examples:
const newMessageSub = this.messageListener('added')
.subscribe(async (value) => {
This file has been truncated. show original
1 Like