ios 3上[NSOperationQueue mainQueue]的替代方案

时间:2011-05-12 08:14:31

标签: objective-c ios nsoperationqueue ios3.0

在ios3上等效的这些操作

[NSOperationQueue mainQueue];
[NSOperationQueue currentQueue];

2 个答案:

答案 0 :(得分:1)

确实没有+ currentQueue的等价物。对于+ mainQueue,你要打电话

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait

使用包含需要在主线程上完成的工作的方法。

答案 1 :(得分:0)

除了自己动手之外别无选择。

像这样的 可能 工作:(未经测试)

@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