From be477fb59196948eb30f96b1ee0ce4b66055a995 Mon Sep 17 00:00:00 2001 From: Kay Date: Thu, 16 Oct 2014 15:28:34 +0900 Subject: [PATCH] added @mainthread macro --- MPFastDispatch.h | 5 +++++ README.md | 21 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/MPFastDispatch.h b/MPFastDispatch.h index 1abc1cb..abaf368 100644 --- a/MPFastDispatch.h +++ b/MPFastDispatch.h @@ -47,6 +47,11 @@ autoreleasepool {} \ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0),BLOCK); \ +#define mainthread(BLOCK) \ + autoreleasepool {} \ + if ([NSThread isMainThread]) BLOCK(); \ + else dispatch_async(dispatch_get_main_queue(),BLOCK); \ + #define sem(TAG, VAL) \ autoreleasepool {} \ __block dispatch_semaphore_t __sem##TAG = \ diff --git a/README.md b/README.md index 6598b30..651e1c2 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,9 @@ Import the class in your prefix: @once(BLOCK) //"dispatch_once" shorthand @after(SECONDS,BLOCK) //"dispatch_after" shorthand - @mainqueue(BLOCK) //Executes a block on the main queue + @mainqueue(BLOCK) //Executes a block asynchronously on the main queue @backgroundqueue(BLOCK) // Executes a block on the global background queue + @mainthread(BLOCK) //Executes a block on the main thread @sem(TAG,VAL) //Creates a semaphore named "__sem##TAG" with value "VAL" @sem_signal(TAG) //Increments value of semaphore named "__sem##TAG" @@ -53,6 +54,24 @@ With **MPFastDispatch** you can do: CODE }) +##Excute a block on the main thread + +To excute a block on the main thread, you would do... + + if ([NSThread isMainThread]) { + CODE + } else { + dispatch_async(dispatch_get_main_queue(), ^{ + CODE + }); + } + +With **MPFastDispatch** you can do: + + @mainthread(^{ + CODE + }) + ##Dispatch a block once To dispatch a block once you would do...