【问题标题】:How to store (in a variable) the Firestore document data that was read from database in Xcode如何存储(在变量中)从 Xcode 中的数据库读取的 Firestore 文档数据
【发布时间】:2019-04-05 16:08:33
【问题描述】:

我正在尝试从 Firebase Firestore 读取数据,但无法将文档数据存储在变量中,因为这些变量只是本地变量,并且会被“垃圾收集”

我尝试创建一个尝试返回文档内容的函数,但由于“getDocument”方法不允许返回类型,因此导致错误。

docRef.getDocument { (document, error) in
    if let document = document, document.exists {
        let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
        var dataValues = document.data()!.values
        self.textInput = dataDescription
        //I first tried doing it without the word self, but it gave an error insisting that I add self, but adding it makes it a local variable and not the String variable defined in the class
        //textInput is a String
        print("Document data: \(document.data()!.values)")
        //This will print out everything in the document
    } else {
        print("Document does not exist")
    }
}

我正在尝试查看是否可以为文档数据设置任何变量,并且在读取数据库后仍然能够访问数据。

【问题讨论】:

    标签: ios swift firebase google-cloud-firestore


    【解决方案1】:

    我建议你看一下https://firebase.google.com/docs/firestore/manage-data/transactions,Firebase 拥有最完整的文档之一,这将对你有所帮助,确切地说你应该这样做:

    let sfReference = db.collection("cities").document("SF")
    
    db.runTransaction({ (transaction, errorPointer) -> Any? in
        let sfDocument: DocumentSnapshot
        do {
            try sfDocument = transaction.getDocument(sfReference)
        } catch let fetchError as NSError {
            errorPointer?.pointee = fetchError
            return nil
        }
    
        guard let oldPopulation = sfDocument.data()?["population"] as? Int else {
            let error = NSError(
                domain: "AppErrorDomain",
                code: -1,
                userInfo: [
                    NSLocalizedDescriptionKey: "Unable to retrieve population from snapshot \(sfDocument)"
                ]
            )
            errorPointer?.pointee = error
            return nil
        }
    
        // Note: this could be done without a transaction
        //       by updating the population using FieldValue.increment()
        transaction.updateData(["population": oldPopulation + 1], forDocument: sfReference)
        return nil
    }) { (object, error) in
        if let error = error {
            print("Transaction failed: \(error)")
        } else {
            print("Transaction successfully committed!")
        }
    }
    

    如 Firebase 文档中所述。

    【讨论】:

    • 我知道交易,但我正在尝试为我刚刚读取的数据分配一个变量。每当我尝试为从数据库读取的数据分配一个变量时,它会报错并坚持添加关键字“self”,这将创建一个新变量,并在读取数据库完成后将被“垃圾收集”。如果我的措辞不好,我的问题是:有什么方法可以为我从 Firebase Firestore 读取的数据分配一个变量
    • 我不太了解 swift 但是,当我下载文档时,我会执行 doc.getDocument().toObject(nameClass.class) 并获得一个包含数据的变量(该类应该有文档的相同结构),然后我直接用.set(变量)上传变量,知道,您可以将文档中的数据存储在一个变量中(具有相同的结构)并将另一个变量分配给该变量,然后上传,我想,希望这会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 2017-07-22
    相关资源
    最近更新 更多