异步处理6 使用NSOperationQueue

作者: Dayu大鱼 | 来源:发表于2016-07-16 00:09 被阅读94次

    使用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];
            }];
        }];
        
    }
    
    
    

    相关文章

      网友评论

        本文标题:异步处理6 使用NSOperationQueue

        本文链接:https://www.haomeiwen.com/subject/qmtujttx.html