Swift wait for closures to finish

Viewed 2635

I have a problem with asynchronous tasks.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
    self.loadRemote {
        log.debug("completed remote data")
    }
    self.loadLocal {
        log.debug("completed local data")
    }
    self.loadAdmin {
        log.debug("completed admin data")
    }
    dispatch_async(dispatch_get_main_queue()) {
        log.debug("called completed data")
        completed()
    }
}

The following code does not work as I want to. It gets called in the following order:

completed local data
called completed data
completed remote data

Which is not what I expected to. You can probably imagine that I wanted the order: "local", "remote" and finally "called completed".

Thanks!

1 Answers
Related