0

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.

9
  • Is the only problem that the DispatchGroup is not behaving correctly? Or is there an operation issue also with how your web requests are processed? Commented Jul 4, 2017 at 18:21
  • It's because you're just printing "loop finished" when you're done submitting the requests. As J. Doe says, if you want to be notified when all of these dispatch groups are done, use group.notify. Commented Jul 4, 2017 at 18:26
  • Unrelated, but if either logo?.data was not nil or if the unwrapping of the URL failed, your group.enter() and group.leave() calls will be unbalanced, and your group will never finish. That's not the issue here, but you should be wary of this. You want to make sure that the enter and leave calls will always be balanced, one-for-one. Commented Jul 4, 2017 at 18:28
  • @ AgnosticDev: No there is not operating issue. The code works fine for up to 10 values. But running it with 100, the app crashes. So the requests are not executed asynchronously. Commented Jul 4, 2017 at 18:34
  • @ Rob: So you think I should add a second group.leave()? Like I edited in the code? Commented Jul 4, 2017 at 18:38

1 Answer 1

3

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")
}
}
Sign up to request clarification or add additional context in comments.

9 Comments

I just tried it, but it doesn´t change anything. This is after/outside the loop, but I need to pause the loop inside itself.
@ J. Doe: I just tried it. Unfortunatelly it doesn´t.
@ J. Doe: Still, the second half of my output is a long row of stored leave, stored leave
@ J. Doe: At least it should be enter stored leave, enter stored leave , right?
@ J. Doe: So in the first part of my output, I´m getting all enters at once, in the second half i´m getting the leaves. Shouldn´t it be, that no new enter is executed as long as the last leave is executed?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.