-
Notifications
You must be signed in to change notification settings - Fork 0
/
TLMainThreadPerformer.m
85 lines (69 loc) · 1.86 KB
/
TLMainThreadPerformer.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// TLMainThreadPerformer.m
// Mercatalog
//
// Created by Nathan Vander Wilt on 1/14/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "TLMainThreadPerformer.h"
// based on http://nifty-box.com/blog/2006/12/nsinvocation-cleans-code.html
@interface TLMainThreadPerformer : NSObject {
@private
id target;
BOOL wait;
}
@end
@implementation TLMainThreadPerformer
- (id)initWithTarget:(id)theTarget shouldWait:(BOOL)shouldWait {
self = [super init];
if (self) {
target = [theTarget retain];
wait = shouldWait;
}
return self;
}
- (void)dealloc {
[target release];
[super dealloc];
}
- (BOOL)respondsToSelector:(SEL)aSelector {
BOOL responds = [super respondsToSelector:aSelector];
if (!responds) {
responds = [target respondsToSelector:aSelector];
}
return responds;
}
- (NSMethodSignature*)methodSignatureForSelector:(SEL)aSelector {
NSMethodSignature* signature = [super methodSignatureForSelector:aSelector];
if (!signature) {
signature = [target methodSignatureForSelector:aSelector];
}
return signature;
}
- (void)mainThreadInvoke:(NSInvocation*)theInvocation {
[theInvocation invokeWithTarget:target];
}
- (void)forwardInvocation:(NSInvocation*)anInvocation {
if ([[NSThread currentThread] isMainThread]) {
[anInvocation invokeWithTarget:target];
}
else {
[anInvocation retainArguments];
[self performSelectorOnMainThread:@selector(mainThreadInvoke:)
withObject:anInvocation
waitUntilDone:wait];
}
}
@end
@implementation NSObject (TLMainThreadProxy)
- (id)tlMainThreadProxy {
TLMainThreadPerformer* proxy = [[TLMainThreadPerformer alloc] initWithTarget:self
shouldWait:NO];
return [proxy autorelease];
}
- (id)tlMainThreadWait {
TLMainThreadPerformer* proxy = [[TLMainThreadPerformer alloc] initWithTarget:self
shouldWait:YES];
return [proxy autorelease];
}
@end