【问题标题】:How can I return a function only when the async call in a loop is completed in SwiftUI and Firebase?只有在 SwiftUI 和 Firebase 中完成循环中的异步调用时,如何才能返回函数?
【发布时间】:2020-07-25 09:33:04
【问题描述】:

我想要一个函数,当用户作为参数传入时,它会返回用户所做的评论列表。但是,我意识到该函数返回一个空数组,因为对数据库的调用仅在函数返回后运行。

我在网上做了一些搜索并尝试使用 DispatchGroup,但不确定我是否正确应用它。

不确定我是否足够清楚,但这是我的代码的 sn-p:

func getAllReviews(user: User) {
        let reviewID = user.reviews
        var reviews: [Review] = [Review]()
        let group = DispatchGroup()
        group.enter()
        for i in reviewID {
            print(i)
            db.collection("reviews").document(i).getDocument {
                (query, err) in
                print("alive??")
                DispatchQueue.main.async {
                    if err != nil {
                        print("hello")
                    } else {
                        print("did it reach")
                 
                        let userId = query!.get("user id") as? String ?? ""
                       
                        let star = query!.get("star") as? Int ?? -1
                        let value = query!.get("value") as? Int ?? 0
                        let comments = query!.get("comments") as? String ?? ""
                        print(comments)
                        reviews.append(Review(id: i,userId: userId,  star: star, value: value, comments: comments))
                        print(reviews)
                        print("does it come")
                    }
                }
                group.leave()
            }
        }
        group.notify(queue: .main) {
            completion(reviews)
        }
    }

不胜感激,提前谢谢你!

【问题讨论】:

    标签: swift firebase asynchronous google-cloud-firestore swiftui


    【解决方案1】:

    enter 行在错误的位置,它必须在循环内。

    并且不需要DispatchQueue.main 闭包

    func getAllReviews(user: User) {
        let reviewID = user.reviews
        var reviews: [Review] = [Review]()
        let group = DispatchGroup()
        
        for i in reviewID {
            print(i)
            group.enter()
            db.collection("reviews").document(i).getDocument {
                (query, err) in
                print("alive??")
                
                if err != nil {
                    print("hello")
                } else {
                    print("did it reach")
                 
                    let userId = query!.get("user id") as? String ?? ""
                       
                    let star = query!.get("star") as? Int ?? -1
                    let value = query!.get("value") as? Int ?? 0
                    let comments = query!.get("comments") as? String ?? ""
                    print(comments)
                    reviews.append(Review(id: i,userId: userId,  star: star, value: value, comments: comments))
                    print(reviews)
                    print("does it come")
                }
                group.leave()
            }
        }
        group.notify(queue: .main) {
            completion(reviews)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-14
      • 2019-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      相关资源
      最近更新 更多