2

I have a database with users and their messages, but when I attempt to convert the users date into the predefined tableview. Here is the code. I am getting two messages outputted. The one in the title as well as TIC Read Status [2:0x0]: 1:57 .

I have check the output of the code to some extent.

import UIKit
import FirebaseFirestore

class ChatsVC: UIViewController,  UITableViewDelegate, UITableViewDataSource{

    @IBOutlet weak var chatsTableView: UITableView!

    var recentChats: [NSDictionary] = [] 

    //TableViewDataSource

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print ("here is the \(recentChats.count)")
        return recentChats.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! RecentChatTableViewCell
        let recent = recentChats[indexPath.row]
        cell.generateCell(recentChat: recent, indexPath: indexPath)
        return cell
    }

    //MARK: LoadRecentChats

    func loadRecentChats() {
        recentListener = reference(.Recent).whereField(kUSERID, isEqualTo: FUser.currentId()).addSnapshotListener({ (snapshot, error) in

            guard let snapshot = snapshot else { return }
            print ("snapshot count is \(snapshot.count)")
            print ("\(snapshot)")

            self.recentChats = []

            if !snapshot.isEmpty {
                let sorted = ((dictionaryFromSnapshots(snapshots: snapshot.documents)) as NSArray).sortedArray(using: [NSSortDescriptor(key: kDATE, ascending: false)]) as! [NSDictionary]

                for recent in sorted {
                    if recent[kLASTMESSAGE] as! String != "" && recent[kCHATROOMID] != nil && recent[kRECENTID] != nil {
                    self.recentChats.append(recent)
                }
            }
            self.chatsTableView.reloadData()
        }
    }
 )}

}

I have edited down the code posted here from the original file, but have left a large chunk as I only know vaguely where the error stems from. Snapshot prints out properly however when it is converted to recentChats.count, it prints 0 regardless.

Olympiloutre
  • 2,268
  • 3
  • 28
  • 38

0 Answers0