zoukankan      html  css  js  c++  java
  • 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:

  • 相关阅读:
    chapter16 计算机体系结构基础
    ASP.NET 2.0加密Web.config 配置文件
    用IIS6.0的Kernel Caching 压缩技术提高应用程序性能
    .net中怎样执行一个字符串
    ASP.NET 页面生存周期中的关键事件
    ASP.NET获取客户端IP及MAC地址
    DOS命令关闭计算机
    1个式子检测密码强度
    徐州话六级考试
    .net2.0中新增的Substitution控件
  • 原文地址:https://www.cnblogs.com/yk00/p/2960546.html
Copyright © 2011-2022 走看看