aslanyanhaik / Quick-Chat

Real time chat app written in Swift 5 using Firebase

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

You can improvise the downloadLastMessage Method with this simple way

anvpn opened this issue · comments

Hi there.

I have clone your code and i really appreciated the work you had done. It was really nice and well-explained work you have provide to a beginner.

But i have found some more easy and clean code with some of your implement method and will be improvised.

You have one method named : "downloadLastMessage" in Message Class

Here for having the last message of chat/conversation you have loop all messages and get last one , we can achieved same by query firebase database to have last message.

Your code
Database.database().reference().child("conversations").child(forLocation).observe(.value, with: { (snapshot) in

You can replace this with following code which only fetch last record without having loop
Database.database().reference().child("conversations").child(forLocation).queryLimited(toLast: 1).observe(.value, with: { (snapshot) in

Simply :)

I have tested same

Thank you :)

@AnitaVPN I have changed the project which has fixed this.