【问题标题】:getdocuments function not stoppinggetdocuments 功能没有停止
【发布时间】:2022-06-15 04:10:02
【问题描述】:

所以我正在尝试使用此函数从 Firestore 获取数据L

static func getData() -> [Post]{
    let db = Firestore.firestore()
    var posts = [Post]()
    db.collection("Posts").getDocuments { querySnapshot, error in
        guard let snapshot = querySnapshot else {
            print("Error retreiving snapshots \(error!)")
            return
        }
        
        for document in snapshot.documents{
            posts.append(Post(t: document.data()["title"] as? String ?? "", a: document.data()["author"] as? String ?? "", pm: document.data()["priceMax"] as? Double ?? 0.0, c: document.data()["content"] as? String ?? ""))
            print("New Post... ")
            print(document.data())
            print(posts.count)
            print(posts[0].title)
            }
        return
    }
    print("test")
    return posts
}

从打印语句中我可以看出它获取了数据,但函数永远不会结束。 print("test") 永远不会运行,因此帖子永远不会返回。如何更改它以返回数据?

【问题讨论】:

  • 查找异步函数。测试打印和return posts 在其他两个打印语句之前运行。您需要一个完成处理程序。

标签: swift firebase google-cloud-firestore swiftui


猜你喜欢
  • 1970-01-01
  • 2017-09-26
  • 1970-01-01
  • 1970-01-01
  • 2016-04-09
  • 2012-08-10
  • 1970-01-01
  • 2016-06-28
  • 2012-01-10
相关资源
最近更新 更多