使用NSOperationQueue实现异步处理
@implementation ViewController
{
NSOperationQueue*mainQueue;
NSOperationQueue*serialQueue;
}
- (void)viewDidLoad {
[super viewDidLoad];
mainQueue=[NSOperationQueue mainQueue];
serialQueue=[[NSOperationQueue alloc]init];
serialQueue.maxConcurrentOperationCount=1;
...
-(void)bigTaskAction{
[serialQueue addOperationWithBlock: ^{
[mainQueue addOperationWithBlock: ^{
[self.myActivityIndicator startAnimating];
}];
int updateUIWhen=1000;
for (int i=0;i<10000;i++) {
NSString*newString=[NSString stringWithFormat:@"i=%i",i ];
NSLog(@"%@ ",newString);
if (i==updateUIWhen) {
float f =(float)i/10000;
NSNumber*percentDone=[NSNumber numberWithFloat:f];
[mainQueue addOperationWithBlock: ^{
[self.myProcessView setProgress:[percentDone floatValue ]animated:YES];
}];
updateUIWhen=updateUIWhen+500;
}
}
[mainQueue addOperationWithBlock:^{
[self.myProcessView setProgress:1.0 animated:YES];
[self.myActivityIndicator stopAnimating];
}];
}];
}