Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Comparing dispatch_get_current_queue and dispatch_get_main_queue is explicitly forbidden by the documentation #20

Merged
merged 2 commits into from

2 participants

@JaviSoto

Checking the right way if a method is invoked from the main thread. The GCD documentation states: (dispatch/queue.h)

When dispatch_get_current_queue() is called on the main thread, it may
or may not return the same value as dispatch_get_main_queue(). Comparing
the two is not a valid way to test whether code is executing on the
main thread.

@Cocoanetics Cocoanetics merged commit 45832cb into Cocoanetics:master
@Cocoanetics
Owner

I guess your approach is safer. Thank you for enhancing it!

@Cocoanetics
Owner

Actually you had a bug there. the [self isMainThread] always returns true because [NSThread mainThread] always returns a valid main thread. I fixed that in a later commit and removed the single-statement method. e5a4574

Not to worry. Either way thank you very much for your impetus! I learned quite a bit from this, even though it is slightly embarrassing having to go back into an article that many people have read and liked and make changes stemming from my own ignorance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 8, 2013
  1. @JaviSoto

    comparing dispatch_get_current_queue and dispatch_get_main_queue is e…

    JaviSoto authored
    …xplicitely forbidden by the documentation
  2. @JaviSoto
This page is out of date. Refresh to see the latest.
Showing with 7 additions and 3 deletions.
  1. +7 −3 Core/Source/iOS/UIView+DTDebug.m
View
10 Core/Source/iOS/UIView+DTDebug.m
@@ -11,6 +11,10 @@
@implementation UIView (DTDebug)
+- (BOOL)_isMainThread
+{
+ return [NSThread mainThread];
+}
- (void)methodCalledNotFromMainQueue:(NSString *)methodName
{
@@ -19,7 +23,7 @@ - (void)methodCalledNotFromMainQueue:(NSString *)methodName
- (void)_setNeedsLayout_MainQueueCheck
{
- if (dispatch_get_current_queue() != dispatch_get_main_queue())
+ if (![self _isMainThread])
{
[self methodCalledNotFromMainQueue:NSStringFromSelector(_cmd)];
}
@@ -30,7 +34,7 @@ - (void)_setNeedsLayout_MainQueueCheck
- (void)_setNeedsDisplay_MainQueueCheck
{
- if (dispatch_get_current_queue() != dispatch_get_main_queue())
+ if (![self _isMainThread])
{
[self methodCalledNotFromMainQueue:NSStringFromSelector(_cmd)];
}
@@ -41,7 +45,7 @@ - (void)_setNeedsDisplay_MainQueueCheck
- (void)_setNeedsDisplayInRect_MainQueueCheck:(CGRect)rect
{
- if (dispatch_get_current_queue() != dispatch_get_main_queue())
+ if (![self _isMainThread])
{
[self methodCalledNotFromMainQueue:NSStringFromSelector(_cmd)];
}
Something went wrong with that request. Please try again.