向swift数组添加元素时出现问题

dm7nw8vv  于 2022-12-17  发布在  Swift
关注(0)|答案(1)|浏览(143)

我正在创建一个应用程序,我向firebase存储发出请求,然后将结果添加到数组中,但最后当从viewDidLoad或其他函数显示数组时,我得到的是一个空数组,但如果在请求后立即做出结论,则所有内容都正确显示
'

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)
                    }
                }
                
            }
    }
    

}

'
我正在创建一个应用程序,我向firebase存储发出请求,然后将结果添加到数组中,但最后当从viewDidLoad或其他函数显示数组时,我得到的是一个空数组,但如果在请求后立即做出结论,则所有内容都正确显示

vulvrdjw

vulvrdjw1#

从Firebase获取数据是一个异步的过程,所以这里你应该在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
                
            }
    }

相关问题