How to set up DispatchGroup in asynchronous iteration?

时光总嘲笑我的痴心妄想 提交于 2019-12-20 06:10:24

问题


I´m trying to set up an iteration for downloading images. The whole process works, but taking a look in the console´s output, something seems to be wrong.

func download() {

    let logos = [Logos]()
    let group = DispatchGroup()

    logos.forEach { logo in

        print("enter")
        group.enter()

        if logo?.data == nil {

            let id = logo?.id as! String

            if let checkedUrl = URL(string: "http://www.apple.com/euro/ios/ios8/a/generic/images/\(id).png") {

                print(checkedUrl)

                LogoRequest.init().downloadImage(url: checkedUrl) { (data)  in

                    logo?.data = data
                    print("stored")

                    group.leave()
                    print("leave")
                }
            }
        }
    }

    print("loop finished")
}

Output:

enter
http://www.apple.com/euro/ios/ios8/a/generic/images/og.png
enter
http://www.apple.com/euro/ios/ios8/a/generic/images/eg.png
enter
http://www.apple.com/euro/ios/ios8/a/generic/images/sd.png
enter
http://www.apple.com/euro/ios/ios8/a/generic/images/hd.png
loop finished
stored
leave
stored
leave
stored
leave
stored
leave

It looks like the iteration does not care about entering and leaving the DispatchGroup() at all. The webrequests are fired almost at the same time. In my opinion the output should look like this:

enter
http://www.apple.com/euro/ios/ios8/a/generic/images/og.png
stored
leave
enter
http://www.apple.com/euro/ios/ios8/a/generic/images/eg.png
stored
leave
...
loop finished

Did I oversee something? Would be awesome to get some ideas.


回答1:


What about this:

group.notify(queue: .main) {
print("loop finished")
}

Instead of your normal print.

edit:

func download() {

let logos = [Logos]()  // NSManagedObject
let group = DispatchGroup()

logos.forEach { logo in


    if logo?.data == nil {
        let id = logo?.id as! String
        if let checkedUrl = URL(string: "http://www.apple.com/euro/ios/ios8/a/generic/images/\(id).png") {

            print(checkedUrl)
             print("enter")
            group.enter()
            LogoRequest.init().downloadImage(url: checkedUrl) { (data)  in
                //this is async I think

                coin?.logo = data
                print("stored")

                group.leave()
                print("leave")
            }
        }
    }
}

group.notify(queue: .main) {
print("loop finished")
}
}


来源:https://stackoverflow.com/questions/44912000/how-to-set-up-dispatchgroup-in-asynchronous-iteration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!