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()
}
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
Call it after the end point of the for loop.
You have the following line:
But look at the output of
print(data)
for thedateCreated
value:The value is an instance of
FIRTimestamp
but you are trying to cast it as aDouble
. This cast is failing so the wholeif let
statement is false. This is why you never append any values toself.roomName
.Update your
if let
statement by changingas? Double
toas? FIRTimestamp
. Then use whatever APIFIRTimestamp
provides to get the desiredDouble
value from it where you create theNewRoom
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 callreloadData
for each loop iteration. Just call it once after fully updating the array.