Alternative for [NSOperationQueue mainQueue] on ios 3
What is the equivalent of these operations on ios3
[NSOperationQueue mainQueue];
[NSOperat开发者_Python百科ionQueue currentQueue];
There really wasn't an equivalent for +currentQueue. For +mainQueue you'd call
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
with a method that contained the work that needed to be done on the main thread.
There is no other alternative other than rolling your own.
Something like this might work: (untested)
@interface NSOperationQueue(MainQueueAdditions)
+ (NSOperationQueue *) mainQueue;
@end
@implementation NSOperationQueue(MainQueueAdditions)
+ (NSOperationQueue *) mainQueue {
static NSOperationQueue *queue = nil;
if(queue == nil) queue = [[NSMainOperationQueue alloc] init];
return queue;
}
@end
@interface NSMainOperationQueue : NSOperationQueue {}
@end
@implementation NSMainOperationQueue
- (void) addOperation:(NSOperation *) operation {
[self queueOperationInternal:operation];
}
- (void) addOperationWithBlock:(void (^)(void))block {
[self queueOperationInternal:[NSBlockOperation blockOperationWithBlock:block]];
}
- (void) queueOperationInternal:(NSOperation *) operation {
[[NSRunLoop mainRunLoop] performSelector:@selector(start) target:operation argument:nil order:-[operation queuePriority] modes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
}
@end
精彩评论