-
Notifications
You must be signed in to change notification settings - Fork 9
/
NSThread+Blocks.m
63 lines (56 loc) · 1.53 KB
/
NSThread+Blocks.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// NSThread+Blocks.m
// Shopify_Mobile
//
// Created by Matthew Newberry on 9/3/10.
// Copyright 2010 Shopify. All rights reserved.
//
#ifndef __IPHONE_4_0
#import <dispatch/dispatch.h>
#endif
@interface NSThread (BlocksAdditions)
- (void)performBlock:(void (^)())block;
- (void)performBlock:(void (^)())block waitUntilDone:(BOOL)wait;
+ (void)performBlockInBackground:(void (^)())block;
+ (void)performBlockOnMainThread:(void (^)())block;
@end
@implementation NSThread (BlocksAdditions)
- (void)performBlock:(void (^)())block
{
if ([[NSThread currentThread] isEqual:self])
block();
else
[self performBlock:block waitUntilDone:NO];
}
- (void)performBlock:(void (^)())block waitUntilDone:(BOOL)wait
{
[NSThread performSelector:@selector(ng_runBlock:)
onThread:self
withObject:[[block copy] autorelease]
waitUntilDone:wait];
}
+ (void)ng_runBlock:(void (^)())block
{
block();
}
+ (void)performBlockInBackground:(void (^)())block{
#ifdef __IPHONE_4_0
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
block();
});
#else
[NSThread performSelectorInBackground:@selector(ng_runBlock:)
withObject:[[block copy] autorelease]];
#endif
}
+ (void)performBlockOnMainThread:(void (^)())block{
#ifdef __IPHONE_4_0
dispatch_async(dispatch_get_main_queue(), ^{
block();
});
#else
[NSThread performSelectorOnMainThread:@selector(ng_runBlock:)
withObject:[[block copy] autorelease]];
#endif
}
@end