skip to Main Content

I’ve been able to retrieve data from Firestore but my collection is not reloading even after running the "collectionview.reload" method on the main thread. The collectionview works when I input the data manually but returns 0 when I try to populate it with data from Firestone. Please help!

Below is the code for the api call:

func loadRooms() {
        roomName = []
//        print("RoomName Count = ", roomName.count)
       

        db.collection("rooms").getDocuments { querysnapshot, error in
            if let e = error {
                print("Unable to load data, (e)")
            } else {
                if let snapshotDocuments = querysnapshot?.documents {
                    for doc in snapshotDocuments {
                        let data = doc.data()
                        print(data)
                        if let room = data["roomNameLabel"] as? String, let roomDescription = data["roomNameDescription"] as? String, let dateRoomCreated = data["dateCreated"] as? Double {
                            let newRoomDetails = NewRoom(roomNameLabel: room, roomNameDescription: roomDescription, dateCreated: dateRoomCreated)
                            
                            self.roomName.append(newRoomDetails)
                            

                            DispatchQueue.main.async {

                                self.collectionview.reloadData()
                            
                            }
                            

                            


                            }
                        }
                    }
                }
            }
        }

Below is the code for collectionview

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        print("room count = ", roomName.count)

        return roomName.count
    }
    
    
    
    
    //collectionView for the roomPills that holds each roomName created by the user
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let roomCell = collectionView.dequeueReusableCell(withReuseIdentifier: "NavReusableCell", for: indexPath) as! NavRoomCell

        roomCell.NavRoomLabel.text = roomName[indexPath.row].roomNameLabel

        return roomCell
    }

Below is my viewdidload

   override func viewDidLoad() {
        super.viewDidLoad()
        
    
        tableview.dataSource = self
        collectionview.dataSource = self
        collectionview.delegate = self
        
        
        tableview.register(UINib(nibName: "TaskCell", bundle: nil), forCellReuseIdentifier: "ReusableCell")
        collectionview.register(UINib(nibName: "NavRoomCell", bundle: nil), forCellWithReuseIdentifier: "NavReusableCell")
        
   
        loadRooms()
        
    }

console returning 0 when I run the code

Below is the output of the print(data) line

["roomNameDescription": After a long absence, "dateCreated": <FIRTimestamp: seconds=1671137642 nanoseconds=920900000>, "roomNameLabel": brandNew Room] ["roomNameDescription": Man Dem 2, "dateCreated": <FIRTimestamp: seconds=1661365735 nanoseconds=82324000>, "roomNameLabel": Awon Omo Ologo] ["roomNameDescription": Man Dem Again o, "dateCreated": <FIRTimestamp: seconds=1661353330 nanoseconds=367393000>, "roomNameLabel": Benefit Boys] ["roomNameDescription": Man Dem, "dateCreated": <FIRTimestamp: seconds=1661353300 nanoseconds=656065000>, "roomNameLabel": City Boys]

2

Answers


  1. Call it after the end point of the for loop.

    DispatchQueue.main.async {
    
       self.collectionview.reloadData()
                                
    }
    
    Login or Signup to reply.
  2. You have the following line:

    if let room = data["roomNameLabel"] as? String,
       let roomDescription = data["roomNameDescription"] as? String,
       let dateRoomCreated = data["dateCreated"] as? Double {
    

    But look at the output of print(data) for the dateCreated value:

    "dateCreated": <FIRTimestamp: seconds=1671137642 nanoseconds=920900000>

    The value is an instance of FIRTimestamp but you are trying to cast it as a Double. This cast is failing so the whole if let statement is false. This is why you never append any values to self.roomName.

    Update your if let statement by changing as? Double to as? FIRTimestamp. Then use whatever API FIRTimestamp provides to get the desired Double value from it where you create the NewRoom instance.

    I have no experience with Firebase so you will need to lookup those specific details.


    And to reiterate earlier comments – you should move the call to reload the collection view to after the for loop. It makes no sense to call reloadData for each loop iteration. Just call it once after fully updating the array.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search