想用DispatchQueue.global()。异步和main.async,但它不能很好地工作[复制]

问题描述 投票:0回答:1

这个问题已经在这里有一个答案:

我想用异步任务为我用DispatchQueue.global()。异步和DispatchQueue.main.async应用程序,但它不工作。

我想摆脱火力点的数据,然后做出列表,并把它传递给关闭。但在下面的代码,称为定时是完成第一和然后posts.append被调用。

func retrieveData(completion: @escaping ([Post]) -> Void) {

    var posts: [Post] = []
    let postsColRef = db.collection("posts").order(by: "createdAt").limit(to: 3)

    postsColRef.getDocuments() { (querySnapshot, error) in
        if let error = error {
            print("Document data: \(error)")
        } else {
            DispatchQueue.global().async {
                for document in querySnapshot!.documents {
                    let data = document.data()
                    let userId = data["userId"] as? String
                    let postImage = data["postImageURL"] as? String
                    let createdAt = data["createdAt"] as? String

                    let docRef = db.collection("users").document(userId!)

                    docRef.getDocument() { (document, error) in
                        if let document = document, document.exists {

                            let data = document.data()!
                            let userName = data["userName"] as? String

                            let post = Post(
                                userId: userId!,
                                userName: userName!,
                                postImageURL: postImage!,
                                createdAt: createdAt!
                            )

                            print("When append called")
                            posts.append(post)

                        }
                    }
                }
                DispatchQueue.main.async {
                    print("When completion called")
                    print(posts)
                    completion(posts)
                }
            }
        }
    }
}

我想先完成for循环,然后去完成。可能有人给我任何的想法?

swift asynchronous
1个回答
0
投票

我刚刚发现这个问题(Wait until swift for loop with asynchronous network requests finishes executing),并试图下面的代码和它的工作。我为大家谁检查这个问题,很抱歉。从接下来的时间,起初我要去搜索中存在的问题。谢谢。

func retrieveData(completion: @escaping ([Post]) -> Void) {

    var posts: [Post] = []
    let postsColRef = db.collection("posts").order(by: "createdAt").limit(to: 3)
    let group = DispatchGroup()

    postsColRef.getDocuments() { (querySnapshot, error) in
        if let error = error {
            print("Document data: \(error)")
        } else {
            for document in querySnapshot!.documents {
                group.enter()
                let data = document.data()
                let userId = data["userId"] as? String
                let postImage = data["postImageURL"] as? String
                let createdAt = data["createdAt"] as? String
                //投稿に紐づくユーザーデータを取得して合わせてpostArrayに挿入
                let docRef = db.collection("users").document(userId!)

                docRef.getDocument() { (document, error) in
                    if let document = document, document.exists {

                        let data = document.data()!
                        let userName = data["userName"] as? String

                        let post = Post(
                            userId: userId!,
                            userName: userName!,
                            postImageURL: postImage!,
                            createdAt: createdAt!
                        )
                        posts.append(post)
                        group.leave()
                    }
                }
            }
            group.notify(queue: .main) {
                print(posts)
                completion(posts)
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.