iOS学习笔记(8)——GCD初探
2015-04-07 12:34
435 查看
1. AppDelegate.m
2. ViewController.h
3. ViewController.m
4. xib文件放一个textview和button并关联输出口和方法
5. 小结
应用程序启动后,程序会运行10秒。
控制台会显示运行的时间间隔
#import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.rootViewController = [[ViewController alloc] initWithNibName:@"SlowWorderView" bundle:nil]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
2. ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UIButton *startButton; @property (weak, nonatomic) IBOutlet UITextView *resultsTextView; @end
3. ViewController.m
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (NSString *)fetchSomethingFromServer { [NSThread sleepForTimeInterval:1]; NSURL *url = [NSURL URLWithString:@"http://nycode.sinaapp.com/d.php"]; NSError *error = nil; NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSDictionary *arr = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; return [arr objectForKey:@"name"]; } - (NSString *)processData:(NSString *)data { [NSThread sleepForTimeInterval:2]; return [data uppercaseString]; } - (NSString *)calculateFirstResult:(NSString *)data { [NSThread sleepForTimeInterval:3]; return [NSString stringWithFormat:@"Number of chars %lu", [data length]]; } - (NSString *)calculateSecondResult:(NSString *)data { [NSThread sleepForTimeInterval:4]; return [data stringByReplacingOccurrencesOfString:@"E" withString:@"e"]; } - (IBAction)doWork:(id)sender { NSDate *startTime = [NSDate date]; NSString *fetcheData = [self fetchSomethingFromServer]; NSString *processdData = [self processData:fetcheData]; NSString *firstResult = [self calculateFirstResult:processdData]; NSString *secondResult = [self calculateSecondResult:processdData]; NSString *resultsSummary = [NSString stringWithFormat:@"First:[%@]\tSecond:[%@]", firstResult, secondResult]; self.resultsTextView.text = resultsSummary; NSDate *endTime = [NSDate date]; NSLog(@"Complete in %f seconds", [endTime timeIntervalSinceDate:startTime]); } @end
4. xib文件放一个textview和button并关联输出口和方法
5. 小结
应用程序启动后,程序会运行10秒。
控制台会显示运行的时间间隔
相关文章推荐
- iOS学习笔记10(2)—GCD介绍(二): 多核心的性能
- iOS并发编程(GCD)学习笔记
- iOS并发编程(GCD)学习笔记
- iOS学习笔记74- 完整详解GCD系列(三)dispatch_group
- iOS学习笔记40-GCD定时器的实现
- iOS学习笔记-----内存管理初探
- iOS学习笔记10(3)—GCD介绍(三): Dispatch Sources
- iOS学习笔记74-完整详解GCD系列(一)dispatch_async;dispatch_sync;dispatch_async_f;dispatch_sync_f
- iOS学习笔记4-GCDAsyncUdpSocket的使用(实现异步发送接收数据)
- iOS学习笔记74-完整详解GCD系列(四)dispatch_semaphore(信号量)
- iOS学习笔记10(6)—GCD实战2:资源竞争
- iOS学习笔记10(7)—GCD示例源码
- iOS学习笔记5-GCDAsyncUdpSocket中的dispatch_queue_set_specific
- IOS学习笔记51--多线程编程之GCD
- iOS学习笔记1-多线程GCD与block
- iOS学习笔记10(7)—GCD示例源码
- iOS并发编程(GCD)学习笔记
- iOS 多线程学习笔记 —— GCD
- iOS学习笔记<20> iOS中的GCD多线程模型 & ios事件的通知方法
- iOS学习笔记74-完整详解GCD系列(二)dispatch_after;dispatch_apply;dispatch_once