如何将数据从Cloud Firestore快速保存到变量中? [英] How do I save data from cloud firestore to a variable in swift?

查看:33
本文介绍了如何将数据从Cloud Firestore快速保存到变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文档中的特定字段保存到变量中.到目前为止,我的代码:

I want to save a particular field from a document into a variable. My code so far:

func getDocument(path: String, field: String? = "nil") -> some Any{
    var returnVar : Any = "DEFAULT VAL"
    var db: Firestore!
    db = Firestore.firestore()
    
    let docRef = db.document(path)
    docRef.getDocument { (document, error) in
        if let document = document, document.exists {
            if(field != "nil"){
                let property =  document.get("phrase") ?? "nil"
                returnVar = property
                return;
            }
            else{
                let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
                returnVar = dataDescription
                return;
            }
        } else {
            print("Document does not exist")
            returnVar = -1
            return;
        }
    }
    print("Returned val: " + (returnVar as! String))
    return returnVar;
}

但是,似乎我的getDocument方法在从firebase读取数据之前就返回了(来自纯OOP领域,我不知道这是怎么发生的)从调试看来,执行似乎只是跳过了整个docRef.getDocument代码,并且跳转到return语句.只有在函数返回后,docRef.getDocument块中的代码才会被执行(什么?已经返回的函数中的代码如何继续执行?).

It seems however that my getDocument method returns before ever reading the data from firebase(coming from pure OOP land, I have no clue how this even happens) From debugging, it seems execution simply skips over the entire docRef.getDocument code and jumps to the return statement. It is only after the function returns does the code in the docRef.getDocument block get executed(what? how does code in a function that has already returned continue to execute?).

如何将特定字段存储在变量中并返回它?

How can I store a particular field in a variable and return it?

推荐答案

这是因为Firestore函数 getDocument asynchronous 函数,它会立即返回,然后继续返回执行其中的代码.如果要从此处返回特定值,则需要使用 completion Handler .您的功能可能如下所示.

That's because Firestore function getDocument is an asynchronous function and it returns immediately and after that it continues to execute the code inside it. If you want to return a particular value from here, you need to use completion Handler. Your function may look like this.

func getDocument(path: String, field: String? = "nil", completion:@escaping(Any)->()) {
var returnVar : Any = "DEFAULT VAL"
var db: Firestore!
db = Firestore.firestore()

let docRef = db.document(path)
docRef.getDocument { (document, error) in
    if let document = document, document.exists {
        if(field != "nil"){
            let property =  document.get("phrase") ?? "nil"
            returnVar = property
            completion(returnVar)
        }
        else{
            let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
            returnVar = dataDescription
            completion(returnVar)
        }
    } else {
        print("Document does not exist")
        returnVar = -1
        completion(returnVar)
      }
    }
  }

然后在 viewDidLoad 或任何其他类似函数中调用该函数.

Then call the function in viewDidLoad or in any other function like this.

getDocument(path: path, field: field){(value) in
    print(value)
}

您可以在 Completion Handlers 中查看更多信息,在这里

You can checkout more about Completion Handlers here

这篇关于如何将数据从Cloud Firestore快速保存到变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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