• 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:

  • 相关阅读:
    0-1性能测试需求分析
    1-10jmeter关联,正则表达式(待巩固)
    1-9jmeter集合点,并发操作
    1-8.jmeter设置断言(检查点)
    1-6jmeter性能测试基础
    泛型中的协变和逆变
    jsPlumb
    jQuery UI vs Kendo UI & jQuery Mobile vs Kendo UI Mobile
    31天重构
    Visual Studio 小工具
  • 原文地址:https://www.cnblogs.com/yk00/p/2960546.html
Copyright © 2020-2023  润新知