0

I call dispatch_async(dispatch_get_main_queue(),block()) and my block() fails to perform UI interactions, because it IS not getting called in main thread, why?

Note: I use PSPDFUIKitMainThreadGuard

Just a runtume illustration of the issue where a block named showListOfProviders() crash on setting a title of a UIViewController

Schemetrical
  • 5,506
  • 2
  • 26
  • 43
Laszlo
  • 2,803
  • 2
  • 28
  • 33
  • Just for curiosity: is there a particular reason for using `DISPATCH_QUEUE_PRIORITY_LOW` instead of `DISPATCH_QUEUE_PRIORITY_DEFAULT`? – József Vesza May 05 '15 at 10:59
  • I use `DISPATCH_QUEUE_PRIORITY_LOW` to perform server communication – Laszlo May 05 '15 at 11:11
  • I understand that, but why explicitly low instead of the default priority? :) – József Vesza May 05 '15 at 11:13
  • @JózsefVesza i made a macro a long time ago, and actually i don't remember why i used `DISPATCH_QUEUE_PRIORITY_LOW ` instead of `DISPATCH_QUEUE_PRIORITY_DEFAULT `. Should i refactor it? Will be any advantage? Or is it just the way it should be? :) köszi – Laszlo May 05 '15 at 11:51
  • I'm not sure how it would affect your current situation, but unless you have a reason to use the low priority specifically, I think you should stick to the default queue. It will not block the UI, but has a high enough priority not to be preceded by too many other tasks. – József Vesza May 05 '15 at 11:56
  • Removed italics that may distract viewers. – Schemetrical May 08 '15 at 08:31

3 Answers3

1

You have to call UI modification blocks as dispatch_sync() on the main thread, as this, it can refresh UI without being blocked.

David Ansermot
  • 6,052
  • 8
  • 47
  • 82
  • I always used `dispatch_async(dispatch_get_main_queue(), ^{});` to execute on main thread without any issues before. – Laszlo May 05 '15 at 09:16
  • I always used `dispatch_sync(dispatch_get_main_queue(), ^{});` to execute my UI updates without any issues. Main thread can be used in dispatch_async, but for UI update I advice you to use the sync version. Did you tried ? – David Ansermot May 05 '15 at 09:17
  • Using dispatch_sync can easily result a deadlock, so thats why i use only dispatch_async, by the way the difference is: http://stackoverflow.com/a/19822753/1293167 – Laszlo May 05 '15 at 09:31
  • It's a serial queue created on the fly. Not about updating the UI on the main queue... Again, have you tried my answer ?! If you make a right conception, and be aware of you GCD flow, you won't have any deadlock – David Ansermot May 05 '15 at 09:32
0

Here is how you can do this in Swift:

runThisInMainThread { () -> Void in
    // Run the method that crashes in here
}

func runThisInMainThread(block: dispatch_block_t) {
    dispatch_async(dispatch_get_main_queue(), block)
}

Its included as a standard function in my repo, check it out: https://github.com/goktugyil/EZSwiftExtensions

Esqarrouth
  • 38,543
  • 21
  • 161
  • 168
0

You are not actually dispatching to the main thread, from the looks of your question, you are invoking your block and passing the return value of your block, e.g. dispatch_async(queue, yourReturnValue);

It should read: dispatch_async(queue, yourBlock); Not: dispatch_async(queue, yourBlock());

drkibitz
  • 507
  • 5
  • 15