开发者

Alternative for [NSOperationQueue mainQueue] on ios 3

开发者 https://www.devze.com 2023-03-05 03:06 出处:网络
What is the equivalent of these operations on ios3 [NSOperationQueue mainQueue]; [NSOperat开发者_Python百科ionQueue currentQueue];

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
0

精彩评论

暂无评论...
验证码 换一张
取 消