Swift: Firebase 3 - How to Load Entire Chat Log per User (Ep 12)

preview_player
Показать описание
In today's lesson, we'll finally implement the feature that allows us to fetch the entire chat log for each individual user. We'll first implement the didSelect method on our main messages page, figure out which user's chat log to show, then fetch and filter out all the necessary messages. Perhaps a longer coding session than normal so get ready. Enjoy!

Previous Episode 11:

All episodes for Firebase 3 series:

Completed Source Code

Want to learn more about iOS Development? Here are a few additional playlist resources on my channel:

Lets Build YouTube series:

Lets Build AppStore series:

Рекомендации по теме
Комментарии
Автор

For those whose stuck in display uicollectionview cell:
1. Include to your class in ChatLogController.swift
2. the sizeForItemAtIndexPath should now:
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {

return CGSize(width: view.frame.height, height:80)
}

tinhpham
Автор

Just In case anyone is facing the problem of not having the collection view cell appearing in blue or red I've found some solutions that worked for me:

(Solution 1) Check MessageController --> Func ShowChatController.
- Just make sure UICollectionViewFlowLayout()) is set. Not UICollectionView. Also don't forget on your ChatLog class file.

func Users)
    {
        let chatController = UICollectionViewFlowLayout())
        //Reference for chatlog controller user we set as chat log is selected link to chat log
        chatController.user = user
        navigationController?.pushViewController(chatController, animated: true 
    }

(Solution 2) If really above doesn't solve it.. Hack it around using stack overflow solution by calling func layoutCells() and adjust layout accordingly in view did load:

func layoutCells() {
        let layout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 10, right: 10)
        = 5.0
        layout.minimumLineSpacing = 5.0
        layout.itemSize = CGSize(width: - 40)/3, height: - 40)/3))
        = layout
    }

raziframzan
Автор

Finally catching up with this Firebase tutorial. Thanks Brian!, I have learned so much for the last few days, love the way that your code is so organized.

nattakarnosborne
Автор

@Lets Build That App Hey Brian, just wanna let you know that this playlist of tutorials for building a chat app with Firebase is awesome! I'm German and I really have to say that this is the best YoutubeChannel regarding Swift coding that I've found. Nevertheless, there's one thing I'd like to let you know: You're sometimes fetching your users multiple times, f.ex. for the chat-cells. You request the data for the chat-partner a second time, although this data is already within contacts (the contacts array). What I do is to simply write "contacts.where" (and then let Xcode autocomplete it...) and return message.chatPartnerId() == contact.id (where 'contact.id' is the contact within the where-closure-brackets). It's the same structure as sorting an array. Hope this could help a bit; at least for my application, it decreases the loading times a lot -- I am using only one UITableView and reloading the data every time. I don't know whether that's a good or an official solution, but it works for me. Let me know if you (or anyone else) have further questions. xD

JS-vmse
Автор

Brian your videos are AMAZING. I have learned so much from this series! I was wondering, could you do a series incorporating the Google Places API?

chrishuynh
Автор

WOW!!! The next tutorial again. Nice Brian!

yanpainghein
Автор

at 22:00 where you programatically choosing the textView font it might have an error so use this one:
tv.font = UIFont.systemFont(ofSize: 16)
:)

PavlosNicolaou
Автор

Awesome video, I have a conceptual question about which one is the best practice. Is it best to retrieve from firebase as you need the info like you are doing it right now, Or would it be better to retrieve all messages for and from the user and just show them on the corresponding screens?

magonicolas
Автор

amazing brian, can you please upload video for pagination in chat like load earlier messages. I will really appreaciate that.
Thanks

nitak
Автор

Hi Brian, having a ton of fun as always. I got a suggestion:

As we push from MessagesController to ChatLogController I was able to observe ONLY the messages that is relevant by structuring "user-messages" node as [messageID: chatPartnerUID] instead of [messageID: true]. So the difference in a nutshell is:

After observing '"user-messages" my way filters using loop and then single-observes "messages", whereas your way single-observes "messages" and then filters.

Not sure which ones better, to be honest, but thought that my implementation would save nextwork cost as app scales maybe? Can you let me know if you could see shortfalls so I can decide between the two?

Thanks!

seansunyoon
Автор

Hi bran thanks for the information can you please tell how we can implement push notifications for this chat application

pankajgaur
Автор

Absolutely love your videos. I've started to pick up your style because I like it so much! I've been creating an app using storyboard but after watching your videos I've been doing everything in code instead. Is there an easy way to change from storyboard to code if possible?

lukewazeak
Автор

Hi, Great episode.
I was wandering, when you use the iOS 9 constraints are they able to run on device with iOS 7 or 8?

Could you please show an example of defining the constraints to run on old devices as well?

I'm guessing you have to add code to check what version the device is running?

Thanks for all your videos.

louiedoan
Автор

Hey Brian, I cannot get the cells to display in the app. I have looked at your source code, compared it to mine countless times, and it just seems that ChatLogController isn't functioning. In debugging the code, I put print statements in numberOfItemsInSection and cellForItemAtIndexPath. It seems that cellForItemAtIndexPath is never called by the app, but the other is. I have no clue what is going on, any ideas?

RoyalQNY
Автор

Hey Brian, sorry to bother you man, I just need a little bit of help. I'm following your videos, and I want to do something slightly different than what you did in this video. I put a TextField in the cell (inside a container view) with the TextView, and I created another ViewController with a TextView and a TextField (just like the cell) so the user can type in his/her message in the TextView and his/her name in the TextField. But I can't get it to the cell, I can only send it to the Firebase database. There's something wrong with how I adapted the observeMessages function, because it makes the app crash when called. The error says the UICollectionView must be initialized with a non-mil layout parameter. I've researched a lot, but can't figure it out. If you could, please, help. Thanks. Sorry again

lucasgurgel
Автор

My text for each message appears at the very edgeof the UITextView even though I put the constraint to right anchor. It doesn't change dynamically based on the size of the message

eg
Автор

Hey Im getting an error with this part of the code:
if let dictionary = snapshot.value as? [String: AnyObject] {
//Creates a Message Object
let message = Message()

The error is a Thread 1: signal SIGABRT
and the compiler is saying: setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key from.'

I was wondering if you could explain why this is happening and ways I can fix this error

khalilhutchinson
Автор

Hey Brian, I want to fetch the only last 20 message, then if user scroll then it fetch another 20 message. So pleas tell me how can i do?

anujgarg
Автор

Hi, if anyone is still following this tutorial and gets the 'uncaught exception 'NSUknownException'...this class is not key coding-compliant for the key name.'. 

In the updated Swift, we cannot do INSTEAD write --> user.id = chatPartnerId

Also, if anyone had any trouble with displaying the name in the chatLogController when a user is selected from messagesController, on showChatController don't forget to include this line: chatLogController?.user = user because I forgot to do this and spend a handful of my time trying to see where it went wrong.

sumayyah
Автор

Brian can you take a look at my project on github I am saving comments to firebase and they appear in uitableview but when i leave view and come back the comments do not appear... But they are still in firebase

FRANKBOCCIA