Home > Software design >  problem with adding element to swift array
problem with adding element to swift array

Time:12-14

I am creating an application. I make a request to the firebase store, after that I add the result to the array, but in the end, when the array is displayed from viewDidLoad or other functions, I get an empty array. But if you make a conclusion immediately after the request, then everything is displayed correctly

`

import UIKit
import Firebase
import FirebaseStorage
import FirebaseFirestore


    
class CatalogVC: UIViewController {
   
    
    struct Item: Codable {
        var title: String
        var price: Int
        var description: String
        var imageUrl: String
    }
    
    @Published var items: [Item] = []
    
    let database = Firestore.firestore()
    
    @IBOutlet weak var textViewCatalog: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let settings = FirestoreSettings()
        Firestore.firestore().settings = settings
        itemsList()
        print(items)
        showCatalogVC()
    }
    
    @IBAction func showCatalogTapped() {
        
    }
    
    private func showCatalogVC() {
        print("SHOW CATALOG")
        let storyboard = UIStoryboard(name:  "Main", bundle: nil)
        let dvc = storyboard.instantiateViewController(withIdentifier: "CatalogVC") as! CatalogVC
        self.present(dvc, animated: true, completion: nil)
    }
    
    func itemsList(){
        database.collection("catalog")
            .getDocuments { (snapshot, error) in
                self.items.removeAll()
                
                if let snapshot {
                    for document in snapshot.documents{
                        let docData = document.data()
                        let title: String = docData["title"] as? String ?? ""
                        let imageUrl: String = docData["imageUrl"] as? String ?? ""
                        let description: String = docData["description"] as? String ?? ""
                        let price: Int = docData["price"] as? Int ?? 0
                        
                        let item: Item = Item(title: title, price: price, description: description, imageUrl: imageUrl)
                        
                        self.items.append(item)
                    }
                }
                
            }
    }
    

}

`

I am creating an application. I make a request to the firebase store, after that I add the result to the array, but in the end, when the array is displayed from viewDidLoad or other functions, I get an empty array. But if you make a conclusion immediately after the request, then everything is displayed correctly

CodePudding user response:

Getting data from Firebase is asynchronous process. So here you should make everything after loading data in closure database.collection("catalog").getDocuments {...}.

func itemsList(){
        database.collection("catalog")
            .getDocuments { (snapshot, error) in
                self.items.removeAll()
                
                if let snapshot {
                    for document in snapshot.documents{
                        let docData = document.data()
                        let title: String = docData["title"] as? String ?? ""
                        let imageUrl: String = docData["imageUrl"] as? String ?? ""
                        let description: String = docData["description"] as? String ?? ""
                        let price: Int = docData["price"] as? Int ?? 0
                        
                        let item: Item = Item(title: title, price: price, description: description, imageUrl: imageUrl)
                        
                        self.items.append(item)
                    }
                }
                print(self.items) //print items to see them
                //here use items data
                
            }
    }
  • Related