首页 > 解决方案 > 为什么我的 swift firestore getDocument 查询没有返回任何内容?

问题描述

swift 和一般编码的新手,专门使用 swiftUI。

在查看了 swift SDK 的文档后,我尝试实现一个基本查询来检查集合中是否存在文档。我已经阅读了类似问题的答案,但据我所知与我的问题没有直接关系。

这是我指的文档: https ://firebase.google.com/docs/firestore/query-data/get-data

这是代码:

let fullrefplacesIndex = db.collection("users").document(currentUserId!.uid).collection("places").document("placesIndex")
        
        fullrefplacesIndex.getDocument { (document, error) in
            
            print("This line does not get executed")
            
            if let document = document, document.exists {
                let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
                print("places index Document data: \(dataDescription)")
            } else {
                print("places index Document does not exist")
            }
        }
        
        print("This is the first line executed after the getDocument method call.")

它是从文档中逐字提取的,但是当我运行它时,它会执行第一行来调用 getDocument 方法,然后就没有别的了。直到方法调用的尾随关闭之后,才执行 'if let' 可选绑定行或其下方的任何内容。

其他信息:

是因为它是异步运行的吗?我需要添加一个监听器?如果是这样,有人可以指导我如何执行此操作,以便它在收到来自服务器的回调时检查文档是否存在?

谢谢!

标签: swiftasynchronousgoogle-cloud-firestore

解决方案


推荐阅读