2

I am creating an iOS app in Swift that uses Firebase as the backend. The app gets images from Firebase storage using this code:

imageRef.downloadURLWithCompletion{ (URL, error) -> Void in
            print("Firebase sent something back!")
            if (error != nil ) {
                print("couldn't get pic") 
            } else {
                print("about to dispatch async")
                dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { [unowned self] in
                    print("got image")
                }
            }
        }
    }

The line "about to dispatch async" gets printed, but the line "got image" doesn't. Any ideas why dispatch_async isn't working?

Olivia Watkins
  • 526
  • 1
  • 6
  • 17
  • 1
    Looks to me like you should be getting main queue not another queue? try `dispatch_get_main_queue` instead? – brandonscript Jul 14 '16 at 20:04
  • I wanted to get background queue. There's a lot of code after the "got image" line that I took out for simplicity which I'd rather didn't run on the main queue. Is there any reason using the main queue would be better? – Olivia Watkins Jul 14 '16 at 20:16
  • Does it work if you use `dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)` for your background line that's broken? I'm thinking that `QOS_CLASS_USER_INITIATED` doesn't get activated for some reason. – Allison Jul 14 '16 at 20:24
  • Sure that's ok but the log will fire in the background (and perhaps not at all if that queue isn't activated). Try just putting the logging statement itself on the main queue. – brandonscript Jul 14 '16 at 20:40
  • @Sirens I tried that, and it worked three times in a row. The time after that, "got image" only got printed 5 of the 14 times the app ran that block of code. What could make the results so inconsistent? – Olivia Watkins Jul 14 '16 at 20:40
  • Are you calling them in quick succession? If you are you are likely running out of threads – Allison Jul 14 '16 at 20:42
  • What does it mean for a queue to not be activated? What would make a queue not get activated? – Olivia Watkins Jul 14 '16 at 20:45
  • @Sirens It's possible I'm running out of threads. Do you know how to check whether this is the issue and/or avoid that problem? – Olivia Watkins Jul 14 '16 at 20:46
  • @brandonscript Is there any reason that it would be bad for the log to fire in the background? – Olivia Watkins Jul 14 '16 at 20:51
  • Not the guy you're tagging but logging in the background is just fine. – Allison Jul 14 '16 at 20:59
  • No, but depending not the task, it may fire completely out of sync with the rest of what you're doing (could be several seconds or a minute) and you may be missing the logging statement. – brandonscript Jul 14 '16 at 21:28

0 Answers0