Hi,
I have implemented collectionview a similar way, but are having issues when getting the data from and external database (Google Firebase). The function that counts the collectionview cells are performed before the data is stored in the episodesArray.
I guess I have to perform some delay or run the functions in the same thread, but are pretty newbie at this, so any help is much appreciated. THANKS!!!!
I have separated the code into 2 swift files:
- UIViewController.swift
...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! EpisodesRow
cell.categoryID = "siibMVy0vuiXPVW2IZsz" //Temp static solution for testing
cell.getEpisodes()
return cell
}
...
- CategoryRow.swift
...
//Connect to Firebase and Datamodel
let db = Firestore.firestore()
var episodesArray : [Episodes] = Episodes
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print("Count: (episodesArray.count)")
return episodesArray.count
}
func getEpisodes() {
//print(categoryID)
db.collection("Episodes").whereField("EpisodeCatID", isEqualTo: categoryID).getDocuments { (snapshot, error) in
if error != nil {
//error handling
print(error!)
} else {
//Success
for document in (snapshot?.documents)! {
let episodeData = Episodes()
episodeData.EpisodeName = (document.data()["EpisodeName"] as? String)!
episodeData.EpisodeImage = (document.data()["EpisodeImage"] as? String)!
self.episodesArray.append(episodeData)
print(" \(self.episodesArray.count)")
print(" \(self.episodesArray)")
}
}
}
}
...
Hi,
I have implemented collectionview a similar way, but are having issues when getting the data from and external database (Google Firebase). The function that counts the collectionview cells are performed before the data is stored in the episodesArray.
I guess I have to perform some delay or run the functions in the same thread, but are pretty newbie at this, so any help is much appreciated. THANKS!!!!
I have separated the code into 2 swift files:
...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! EpisodesRow
cell.categoryID = "siibMVy0vuiXPVW2IZsz" //Temp static solution for testing
cell.getEpisodes()
return cell
}
...
...
//Connect to Firebase and Datamodel
let db = Firestore.firestore()
var episodesArray : [Episodes] = Episodes
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print("Count: (episodesArray.count)")
return episodesArray.count
}
func getEpisodes() {
//print(categoryID)
db.collection("Episodes").whereField("EpisodeCatID", isEqualTo: categoryID).getDocuments { (snapshot, error) in
if error != nil {
//error handling
print(error!)
} else {
//Success
for document in (snapshot?.documents)! {
let episodeData = Episodes()
episodeData.EpisodeName = (document.data()["EpisodeName"] as? String)!
episodeData.EpisodeImage = (document.data()["EpisodeImage"] as? String)!
self.episodesArray.append(episodeData)
...