I have the following code in my View Controller that’s part of my user favourites logic:
var favouritesModel = FavouritesModel()
var userFavourites = UserFavourites()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
favouritesModel.delegate = self
favouritesModel.getFavourites()
}
override func viewDidAppear(_ animated: Bool) {
setStarButton()
}
func setStarButton() {
if currentUser != nil {
print("userFavourites: \(userFavourites)") // userFavourites has data
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Get a cell
let cell = tableView.dequeueReusableCell(withIdentifier: "IngredientCell", for: indexPath) as! IngredientCell
print("userFavourites: \(userFavourites)") // userFavourites is empty
// Return the cell
return cell
}
favouritesModel.getFavourites()
is my model for retrieving the userFavourites
data from Firestore.
This seems like an issue with scope, but I haven’t been able to figure it out. Why does the userFavourites
variable contain data in func setStarButton()
but not in func tableView
?