3

I'm setting a timer so that after a second passes I reset a value for my keyboard extension. The problem is that I feel like the following call is stalling my UI:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    [self resetDoubleTapBool];
})

Is there an asynchronous way of doing this, or a better way in general? Thanks!

KingPolygon
  • 4,753
  • 7
  • 43
  • 72
  • 2
    No it does not. Its like a reminder. When the alarm goes off, it will select the given queue; in your case "(dispatch_get_main_queue()" and fire the block. – Abubakr Dar Feb 07 '15 at 14:27
  • 3
    "I feel like" Programming is not about what you feel. If in doubt, measure. – matt Feb 07 '15 at 14:38
  • @matt Very good point. Definitely wanted to find out more about other alternatives and use cases as well. Thanks will def use instruments. – KingPolygon Feb 07 '15 at 23:59

2 Answers2

6

The dispatch_after() call itself does not block. At (or shortly after) the appointed time, the block will be submitted to the main queue. Submitting it doesn't block the main thread. When the main thread next runs its run loop or is idle within dispatch_main(), it will execute the block.

IF your -resetDoubleTapBool method takes any appreciable amount of time, that can stall your UI. That's just the same as any code that runs on the main thread. It's not specific to dispatch_after() or any other part of GCD.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • Awww weird I'll definitely do some performance testing then, because ```-resetDoubleTapBool``` is only really change a bool value. Yet maybe there's something else that's happening. – KingPolygon Feb 07 '15 at 23:58
  • Is there any chance that you're doing that `dispatch_after()` call many, many times? Basically, are you flooding the main queue with individually-cheap-but-collectively-expensive blocks? – Ken Thomases Feb 08 '15 at 04:57
0

According to the function documentation:

This function waits until the specified time and then asynchronously adds block to the specified queue.

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
user523234
  • 14,323
  • 10
  • 62
  • 102
  • 1
    My apologies: I down-voted because this fails to answer the question (would thread be blocked or not) and simply quotes an ambiguous reference from the docs. The phrase "waits until the specified time" could very easily be misinterpreted to say that the thread will be blocked until that time, which is not the case. (The term "wait" is often used in other contexts, e.g. `dispatch_group_wait`, to mean that the queue is blocked until some point in the future, unlike how the term is used here.) Bottom line, this simply doesn't answer the question. – Rob Feb 08 '15 at 06:27
  • @Rob: OK. As a technical person when I read this Apple's document on this command, it is quite clear. But I think you may have point if you interpreted that way. However, your injection of dispatch_group_wait to support your argument is irrelevant if not ridiculous. The word "wait" were used in total different context. One is part of a description. And The other is the name command itself. BTW: I did not base my answer on the name of the command but supported by evident Apple document. – user523234 Feb 09 '15 at 14:00