E
E
Elsh Yff2016-01-27 14:42:29
iOS
Elsh Yff, 2016-01-27 14:42:29

How will blocks behave with such a sequence?

How will blocks behave with such a sequence, what will be the result?

dispatch_async(dispatch_get_main_queue(), { () -> Void in
    dispatch_sync(dispatch_get_main_queue(), { () -> Void in
        for j in 0...10 {
            print("alpha \(j)")
        }
    })
    for i in 0...10 {
        print("beta \(i)")
        if i == 4 {
            dispatch_sync(dispatch_get_main_queue(), { () -> Void in
                for j in 0...10 {
                    print("gamma \(j)")
                }
            })
        }
    }
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
An, 2016-01-27
@Aznix

There will be a lock , since you are trying to add a synchronous task to the queue, already "being in the queue"

Do not call the dispatch_sync function from a task that is executing on the same queue that you pass to your function call. Doing so will deadlock the queue. If you need to dispatch to the current queue, do so asynchronously using the dispatch_async function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question