在Firestore完成Swift的数据下载之前,如何执行功能?

时间:2019-02-15 08:27:20

标签: swift google-cloud-firestore

我在Google上搜索了很多信息,但仍然没有任何解决方案可以解决我的问题...

我想在UI上显示一个需要Firestore上的数据的列表,但是每次该列表将为空,因为它没有等待Firestore下载。我该怎么解决?

这是我的代码:

private var imgList: [String] = []
override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        downloadfromFilestore()
        bindingView()
}

func downloadfromFirestore(){
    //firestore official code
    for i firestoreDataResult in{
        imgList.append(i.imgURL)
    }
}

func bindingVIew(){
    customElement.setURLs(imgList)
}

我已经尝试了很多解决方案,例如DispatchQueue,DispatchGroup,async ..... 我是Swift的新手,请帮助我。

2 个答案:

答案 0 :(得分:0)

您的function get_config_bytes($val) { $val = trim($val); $last = substr($val, -1); // Get the last character $val = rtrim($val, $last); // Trim away the last character which isn't a number switch(strtolower($last)) { case 'g': $val *= 1024; case 'm': $val *= 1024; case 'k': $val *= 1024; } return $this->fix_integer_overflow($val); } 的官方Firestore快速代码

func downloadFromFirestore()

更多正式的快速Fitestore代码https://firebase.google.com/docs/firestore/query-data/get-data?refresh=1

答案 1 :(得分:0)

您的downloadFromFilestore应该看起来像这样(根据文档):

func downloadFromFiresfore() {
  let docRef = db.collection("cities").document("SF")
  docRef.getDocument { (document, error) in
    if let document = document, document.exists {
        let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
        print("Document data: \(dataDescription)")
        // reload UI here. This part will be executed only when download is completed. 
    } else {
        print("Document does not exist")
    }
  }
}

请注意重新加载UI代码的放置位置。 (我想您已将其放在完成代码块之外)

通常,不建议从视图控制器内部执行数据库访问,而是创建一个服务来管理您的所有调用并可以从任何VC访问的服务。 祝你好运