iphone GCD

View Code
dispatch_async(dispatch_get_global_queue(0, 0), ^{


NSString *fetchedData = [self fetchSomethingFromServer];
NSString *processedData = [self processData:fetchedData];
NSString *firstResult = [self calculateFirstResult:processedData];
NSString *secondResult = [self calculateSecondResult:processedData];


NSString *resultsSummary = [NSString stringWithFormat:
@"First: [%@]\nSecond: [%@]", firstResult,
secondResult];
i++;


dispatch_async(dispatch_get_main_queue(), ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"show result finish");

label.text = resultsSummary;

});


NSDate *endTime = [NSDate date];
NSLog(@"Completed in %f seconds. --->%d",
[endTime timeIntervalSinceDate:startTime],i);
NSLog(@"inside finish");

});

NSLog(@"outside finish");
}

该缺点是只会显示最后一个结果。处理时间10s

 

下面这个添加一个UIapplicationIndicatorView,并把显示结果放到main thread中 处理时间10s

View Code
 btn.enabled = NO;
    //btn.titleLabel.text = @"qq";
    [btn setTitle:@"Loaidng" forState:UIControlStateNormal];
    btn.alpha = 0.3;
    [spinner startAnimating];
    static int i =5;
    NSDate *startTime = [NSDate date];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
    
        
        NSString *fetchedData = [self fetchSomethingFromServer];
        NSString *processedData = [self processData:fetchedData];
        NSString *firstResult = [self calculateFirstResult:processedData];
        NSString *secondResult = [self calculateSecondResult:processedData];
       

     //show result in main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            
            btn.enabled = YES;
            btn.alpha=1.0;
            [spinner stopAnimating];
            //[NSThread sleepForTimeInterval:2];
            NSLog(@"show result finish");
            
            label.text = [[NSNumber numberWithInt:i] stringValue];
            
           });
            
            
            NSDate *endTime = [NSDate date];
            NSLog(@"Completed in %f seconds.   --->%d",
                  [endTime timeIntervalSinceDate:startTime],i);
            NSLog(@"inside finish");

        
        });
        
               
    });
    
     NSLog(@"outside finish");

 

下面这个支持多线程,处理时间7s

View Code
 btn.enabled = NO;
    //btn.titleLabel.text = @"qq";
    [btn setTitle:@"Loaidng" forState:UIControlStateNormal];
    btn.alpha = 0.3;
    [spinner startAnimating];
    static int i =5;
    NSDate *startTime = [NSDate date];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
    
        
        NSString *fetchedData = [self fetchSomethingFromServer];
        NSString *processedData = [self processData:fetchedData];
        //NSString *firstResult = [self calculateFirstResult:processedData];
        //NSString *secondResult = [self calculateSecondResult:processedData];
        __block NSString *firstResult;
        __block NSString *secondResult;
        
        dispatch_group_t group = dispatch_group_create();
        dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
            firstResult = [self calculateFirstResult:processedData];
        });
        
        dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
            secondResult = [self calculateSecondResult:processedData];
        });
        
        dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
            NSString *resultsSummary = [NSString stringWithFormat:
                                        @"First: [%@]\nSecond: [%@]", firstResult,
                                        secondResult];
            i++;
            
            
            // dispatch_async(dispatch_get_main_queue(), ^{
            
            btn.enabled = YES;
            btn.alpha=1.0;
            [spinner stopAnimating];
            //[NSThread sleepForTimeInterval:2];
            NSLog(@"show result finish");
            
            label.text = [[NSNumber numberWithInt:i] stringValue];
            
            //});
            
            
            NSDate *endTime = [NSDate date];
            NSLog(@"Completed in %f seconds.   --->%d",
                  [endTime timeIntervalSinceDate:startTime],i);
            NSLog(@"inside finish");

        
        });
        
               
    });
    
     NSLog(@"outside finish");

 

 background processing isn't support multiasking playground before mid-2009

 

 

block define:

 

posted on 2013-03-14 23:07  fishyk  阅读(156)  评论(0)    收藏  举报

导航