When trying to receive a list of items back from a users favourites collection my array is empty. I have used this code before and im not sure why its not populating.
Below is the ViewController for the favourites im not sure why it is not displaying the favourites. There is two functions that are on the favourites model class. Hopefully someone can spot what I am missing.
//MARK: Outlets
@IBOutlet weak var favouritesTV: UITableView!
@IBOutlet weak var noUserView: UIView!
//MARK: Vars
var favourites : Favourites!
//var allRecipes : [Recipe] = []
let db = Firestore.firestore()
var listener: ListenerRegistration!
var allItems: [Recipe] = []
override func viewDidLoad() {
super.viewDidLoad()
favouritesTV.delegate = self
favouritesTV.dataSource = self
favouritesTV.tableFooterView = UIView()
if User.currentUser() != nil {
loadFavouritesFromFirestore()
print("the favs", allItems)
}else {
showLoginView()
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Checkuser loggin
}
//MARK: - Download favourites
private func loadFavouritesFromFirestore() {
downloadFav(User.currentId()) { (favourites) in
self.favourites = favourites
self.getFavouritesItems()
}
}
private func getFavouritesItems() {
if favourites != nil {
downloadItems(favourites!.recipeIds) { (allItems) in
self.allItems = allItems
self.favouritesTV.reloadData()
}
}
}
//MARK: Actions
//Show Login View
private func showLoginView(){
let loginView = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(identifier: "WelcomeVC")
self.present(loginView, animated: true, completion: nil)
}
//MARK: Table View Func
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = favouritesTV.dequeueReusableCell(withIdentifier: "recipeCell", for: indexPath) as! RecipeCell
cell.configRecipeCell(allItems[indexPath.row])
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
showRecipeView(withRecipe: allItems[indexPath.row])
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let itemToDelete = allItems[indexPath.row]
allItems.remove(at: indexPath.row)
favouritesTV.reloadData()
// removeItemFromBasket(itemId: itemToDelete.id)
updateFavouritesInFirestore(favourites!, withValues: [kRECIPEIDS : favourites!.recipeIds]) { (error) in
if error != nil{
print("error updating favourites", error?.localizedDescription)
}
}
}
}
//MARK: Functions
//Show recipe detail view
private func showRecipeView(withRecipe: Recipe) {
let recipeDetailVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(identifier: "recipeDetailView") as! RecipeDetailsViewController
recipeDetailVC.recipe = withRecipe
self.navigationController?.pushViewController(recipeDetailVC, animated: true)
}
func downloadItems(_ withIds: [String], completion: @escaping (_ itemArray: [Recipe]) ->Void) {
var count = 0
var itemArray: [Recipe] = []
if withIds.count > 0 {
for recipeId in withIds {
FirebaseReference(.Recipe).document(recipeId).getDocument { (snapshot, error) in
guard let snapshot = snapshot else {
completion(itemArray)
return
}
if snapshot.exists {
itemArray.append(Recipe(_dictionary: snapshot.data()! as NSDictionary))
count += 1
} else {
completion(itemArray)
}
if count == withIds.count {
completion(itemArray)
}
}
}
} else {
completion(itemArray)
}
}
}
question from:
https://stackoverflow.com/questions/65927087/my-array-is-empty-when-downloading-data-from-firestore 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…