网络代码完成后如何调用完成

时间:2019-04-01 17:58:02

标签: swift firebase google-cloud-firestore

我有一个查询所有满足多个条件的文档的功能。但是,该查询需要为多个用户运行。从适当的文档中提取所有信息后,我想在转义的完成处理程序中返回所述信息。我对完成处理程序的工作方式仍然有些困惑,但是我设法使它们在其他几个功能中起作用。

func getFriendsEventIds(completion: @escaping (_ friendsEventIds: [String]?) -> ()) {
    getFriendIds { (friendIds) in
        guard let friends = friendIds else { return }
        var friendsEventIds: [String] = []

        for friend in friends {
            let friendEventsQuery = self.db.collection("events").whereField(Constants.EventKeys.memberIds, arrayContains: friend).whereField(Constants.EventKeys.isPrivate, isEqualTo: false)
            friendEventsQuery.getDocuments(completion: { (querySnapshot, error) in
                if let error = error {
                    print("Error getting the friend \(friend)'s events: \(error)")
                } else {
                    for document in querySnapshot!.documents {
                        if let eventId = document.data()[Constants.EventKeys.eventId] as? String {
                            if !friendsEventIds.contains(eventId) {
                                friendsEventIds.append(eventId)
                            }
                        }
                    }
                }
            })
        }
        completion(friendsEventIds)
    }
}

我希望在将数据从完成处理程序中传递出去之前,使用来自查询的数据来填充friendsEventIds。但是,在调试时,我注意到在查询完成之前激活了完成处理程序。

0 个答案:

没有答案
相关问题