GCD工作单元
2016-05-18 17:08
369 查看
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak,nonatomic)IBOutlet UIButton *startBtn;
@property (weak,nonatomic)IBOutlet UITextView *resultsTextView;
@property (weak,nonatomic)IBOutlet UIActivityIndicatorView *spinner;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(NSString *)fetchSomethingFromServer{
[NSThread sleepForTimeInterval:1];
return @"Hi there";
}
-(NSString *)processData:(NSString *)data{
[NSThread sleepForTimeInterval:2];
return [data uppercaseString];
}
-(NSString *)calculateFirstResult:(NSString *)data{
[NSThread sleepForTimeInterval:3];
return [NSString stringWithFormat:@"Number of chars:%lu",(unsigned long)[data length]];
}
-(NSString *)calculateSecondResult:(NSString *)data{
[NSThread sleepForTimeInterval:4];
return [data stringByReplacingOccurrencesOfString:@"E" withString:@"e"];
}
-(IBAction)doWork:(id)sender{
NSDate *startTime = [NSDate date];
//添加指示器代码
self.startBtn.enabled = NO;
[self.spinner startAnimating];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSString *fetchedData = [self fetchSomethingFromServer];
NSString *processedData = [self processData:fetchedData];
NSString *firstResult = [self calculateFirstResult:processedData];
NSString *senconResult = [self calculateSecondResult:processedData];
NSString *resultSummary = [NSString stringWithFormat:@"First:[%@]\nSecond:[%@]",firstResult,senconResult];
dispatch_async(dispatch_get_main_queue(), ^{
self.resultsTextView.text = resultSummary;
});
NSDate *endTime = [NSDate date];
NSLog(@"Completed in %f seconds",[endTime timeIntervalSinceDate:startTime]);
});
// 1 NSString *fetchedData = [self fetchSomethingFromServer];
// NSString *processedData = [self processData:fetchedData];
// NSString *firstResult = [self calculateFirstResult:processedData];
// NSString *senconResult = [self calculateSecondResult:processedData];
// NSString *resultSummary = [NSString stringWithFormat:@"First:[%@]\nSecond:[%@]",firstResult,senconResult];
// self.resultsTextView.text = resultSummary;
// NSDate *endTime = [NSDate date];
// NSLog(@"Completed in %f seconds",[endTime timeIntervalSinceDate:startTime]);
}
@end
@interface ViewController : UIViewController
@property (weak,nonatomic)IBOutlet UIButton *startBtn;
@property (weak,nonatomic)IBOutlet UITextView *resultsTextView;
@property (weak,nonatomic)IBOutlet UIActivityIndicatorView *spinner;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(NSString *)fetchSomethingFromServer{
[NSThread sleepForTimeInterval:1];
return @"Hi there";
}
-(NSString *)processData:(NSString *)data{
[NSThread sleepForTimeInterval:2];
return [data uppercaseString];
}
-(NSString *)calculateFirstResult:(NSString *)data{
[NSThread sleepForTimeInterval:3];
return [NSString stringWithFormat:@"Number of chars:%lu",(unsigned long)[data length]];
}
-(NSString *)calculateSecondResult:(NSString *)data{
[NSThread sleepForTimeInterval:4];
return [data stringByReplacingOccurrencesOfString:@"E" withString:@"e"];
}
-(IBAction)doWork:(id)sender{
NSDate *startTime = [NSDate date];
//添加指示器代码
self.startBtn.enabled = NO;
[self.spinner startAnimating];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSString *fetchedData = [self fetchSomethingFromServer];
NSString *processedData = [self processData:fetchedData];
NSString *firstResult = [self calculateFirstResult:processedData];
NSString *senconResult = [self calculateSecondResult:processedData];
NSString *resultSummary = [NSString stringWithFormat:@"First:[%@]\nSecond:[%@]",firstResult,senconResult];
dispatch_async(dispatch_get_main_queue(), ^{
self.resultsTextView.text = resultSummary;
});
NSDate *endTime = [NSDate date];
NSLog(@"Completed in %f seconds",[endTime timeIntervalSinceDate:startTime]);
});
// 1 NSString *fetchedData = [self fetchSomethingFromServer];
// NSString *processedData = [self processData:fetchedData];
// NSString *firstResult = [self calculateFirstResult:processedData];
// NSString *senconResult = [self calculateSecondResult:processedData];
// NSString *resultSummary = [NSString stringWithFormat:@"First:[%@]\nSecond:[%@]",firstResult,senconResult];
// self.resultsTextView.text = resultSummary;
// NSDate *endTime = [NSDate date];
// NSLog(@"Completed in %f seconds",[endTime timeIntervalSinceDate:startTime]);
}
@end
相关文章推荐
- PHP根据数组的值分组
- 终于解决:升级至.NET 4.6.1后VS2015生成WCF客户端代理类的问题
- Bootstrap的Carousel不能正常播放的几个原因
- python的list,tuple,dist,set和不可变对象
- java的动态代理
- redis lru data 捕捉与存储
- [leetcode] 10. Regular Expression Matching 解题报告
- Scala 专题教程-Case Class和模式匹配(1):简单的示例
- overflow:hidden与position:absolute
- JS判断是否是IE浏览器
- 用Maven部署war包到远程Tomcat服务器
- RESTful Web Service
- 【oschina android源码分析】缓存的设计
- CentOS6.x单用户模式修改root密码
- 一个python发包的脚本
- rpm命令详解
- UiAutomator——如何保存测试异常现场@Before @After
- Java实现在线预览--openOffice实现
- angular 对checkbox选择框的操作实例
- Android 开发框架介绍