简体   繁体   中英

CollectionView not reloading after retrieving data from Firestore

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()
        
    }

运行代码时控制台返回 0

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]

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 as? Double to as? FIRTimestamp 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.

Call it after the end point of the for loop.

DispatchQueue.main.async {

   self.collectionview.reloadData()
                            
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM