如何处理数组IN PARALLEL [英] How to process an array IN PARALLEL

查看:122
本文介绍了如何处理数组IN PARALLEL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,有很多方法可以通过选择器或代码块来运行数组的所有元素。 for(id thing in manyThings).. makeObjectsPerformSelector:.. enumerate ..等等。



如果你有4或16核心,你可能想发送所有的处理到不同的进程。



即使在iOS上,它可能是明智的发送他们所有离开的接口 - 或至少让他们


$ b b

再次,如果您想要将它们发送完成以便随时完成,例如 在执行以下指令之前不要等待枚举完成 ...?

解决方案

如果您可以定位iOS 4+或Mac OS X 10.6+,调度(我使用的类别,因为我认为他们很酷):

  #import< dispatch / dispatch。 h。 

@interface NSArray(DDAsynchronousAdditions)

- (void)makeObjectsPerformSelectorAsynchronously:(SEL)selector;
- (void)makeObjectsPerformSelectorAsynchronously:(SEL)selector withObject:(id)object;

@end

@implementation NSArray(DDAsynchronousAdditions)

- (void)makeObjectsPerformSelectorAsynchronously:(SEL)selector {
[self makeObjectsPerformSelectorAsynchronously :selector withObject:nil];
}

- (void)makeObjectsPerformSelectorAsynchronously:(SEL)selector withObject:(id)object {
for(id element in self){
dispatch_async(dispatch_get_global_queue 0,0),^ {
[element performSelector:selector withObject:object];
});
}
}

@end

//其他地方:

[myArray makeObjectsPerformSelectorAsynchronously:@selector(doFoo)] ;

或者,如果您不想使用类别...

  [myArray enumerateObjectsUsingBlock:^(id obj,NSUInteger index,BOOL * stop){
dispatch_async(dispatch_get_global_queue(0,0),^
[obj performSelector:@selector(doFoo)];
};
}];


So there's a number of ways to run all the elements of an array through a selector or block of code. for (id thing in manyThings).. makeObjectsPerformSelector: .. enumerate.. and so on.

If you have 4 or 16 cores on hand, you may well want to send all the processing off to different processes.

Even on iOS, it could well be sensible to send them all "away" from the interface - or at least let them be finished whenever the wind blows.

What is the best and neatest way to do this in the cocoa milieu?

Again, what if you want to send them away to be finished at any time, i.e., do not wait for the enumeration to finish before executing the following instruction...?

解决方案

If you can target iOS 4+ or Mac OS X 10.6+, use Grand Central Dispatch (and I'm using a category because I think they're cool):

#import <dispatch/dispatch.h>

@interface NSArray (DDAsynchronousAdditions)

- (void) makeObjectsPerformSelectorAsynchronously:(SEL)selector;
- (void) makeObjectsPerformSelectorAsynchronously:(SEL)selector withObject:(id)object;

@end

@implementation NSArray (DDAsynchronousAdditions)

- (void) makeObjectsPerformSelectorAsynchronously:(SEL)selector {
  [self makeObjectsPerformSelectorAsynchronously:selector withObject:nil];
}

- (void) makeObjectsPerformSelectorAsynchronously:(SEL)selector withObject:(id)object {
  for (id element in self) {
    dispatch_async(dispatch_get_global_queue(0,0), ^{
      [element performSelector:selector withObject:object];
    });
  }
}

@end

//elsewhere:

[myArray makeObjectsPerformSelectorAsynchronously:@selector(doFoo)];

Or, if you don't want to use a category...

[myArray enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL * stop) {
  dispatch_async(dispatch_get_global_queue(0,0), ^{
    [obj performSelector:@selector(doFoo)];
  };
}];

这篇关于如何处理数组IN PARALLEL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆