如何从Firebase getDocument函数(Swift)中写入变量 [英] How to write to a variable from within the Firebase getDocument function (Swift)

查看:36
本文介绍了如何从Firebase getDocument函数(Swift)中写入变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阅读一个文档,从该文档中获取一个字段,然后为该字段的值设置一个变量

I want to read a document, get a field from that document, and set a variable to that field's value

我希望将在Firebase getDocument函数外部声明的变量写入其中.实际结果是,该变量正在Firebase getDocument函数中写入,但在函数外部为空.

I expected my variable declared outside the Firebase getDocument function to be written to. The actual results are that the variable is being written to within the Firebase getDocument function but outside the function it is empty.

这是我尝试过的:

[1]:在firebase函数中修改变量-这不起作用对我来说,因为我目前的Swift技能无法很好地翻译

[1]: Modifying variable within firebase function - this didn't work for me because I can't translate it well with my current Swift skills

[2]:如何从Firebase设置变量快照(快速)-这对我来说不起作用,因为实施方式与我现在拥有的有很大出入

[2]: How to set variables from a Firebase snapshot (swift) - this didn't work for me because the implementation deviates by a lot from what I have right now


//open the user Firebase database by documentID
     let userDocument = userdb.collection("users").document(userDocumentId)

     var userjobsData = [[String:Any]]()

     userDocument.getDocument { (docums, error)  in

         if let docum = docums, docum.exists {

         //grab all jobs data
         userjobsData = docum.get("jobData") as! [[String:Any]]

         //sort jobs by category in alphabetical order
         userjobsData = (userjobsData as NSArray).sortedArray(using: [NSSortDescriptor(key: "category", ascending: true)]) as! [[String:AnyObject]]

         }
        //Here userjobsData contains data
        print(userjobsData)
     }
     //Here userjobsData is empty
     print(userjobsData)

推荐答案

实际上,在您的情况下,发生什么情况 Firebase 提取数据是异步任务,并且需要一些时间来提取数据,同时您正在读取 userjobsData ,由于 Firebase 请求尚未完成,因此该数据为空.

Actually what is happening in your case Firebase fetching data is asynchronous task and takes some time to fetching data , in mean time you are reading your userjobsData which is empty since Firebase request has not been completed .

您可以做的实际上是从 firebase 中获取数据之后执行所需的操作.

What you can do is actually perform needed operation after fetching data from firebase .

添加示例代码以供参考.

Adding Sample code for your reference.

private func fetchDataFromFirebase(){

  let userDocument = userdb.collection("users").document(userDocumentId)

     var userjobsData = [[String:Any]]()

     userDocument.getDocument { (docums, error)  in

         if let docum = docums, docum.exists {

         //grab all jobs data
         userjobsData = docum.get("jobData") as! [[String:Any]]

         //sort jobs by category in alphabetical order
         userjobsData = (userjobsData as NSArray).sortedArray(using: [NSSortDescriptor(key: "category", ascending: true)]) as! [[String:AnyObject]]

          self.perfomAction(firebaseResult : userjobsData)
          // now pass this data to your need function like
         }

  }
}

private func perfomAction(firebaseResult : [[String:Any]]){
  // perform your job here
}

这篇关于如何从Firebase getDocument函数(Swift)中写入变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆