iOS学习笔记(8)——GCD初探

1. AppDelegate.m

 1 #import "AppDelegate.h"
 2 #import "ViewController.h"
 3 @interface AppDelegate ()
 4 
 5 @end
 6 
 7 @implementation AppDelegate
 8 
 9 
10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
11     // Override point for customization after application launch.
12     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
13     self.window.rootViewController = [[ViewController alloc] initWithNibName:@"SlowWorderView" bundle:nil];
14     self.window.backgroundColor = [UIColor whiteColor];
15     [self.window makeKeyAndVisible];
16     return YES;
17 }
18 
19 - (void)applicationWillResignActive:(UIApplication *)application {
20     // 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.
21     // 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.
22 }
23 
24 - (void)applicationDidEnterBackground:(UIApplication *)application {
25     // 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.
26     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
27 }
28 
29 - (void)applicationWillEnterForeground:(UIApplication *)application {
30     // 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.
31 }
32 
33 - (void)applicationDidBecomeActive:(UIApplication *)application {
34     // 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.
35 }
36 
37 - (void)applicationWillTerminate:(UIApplication *)application {
38     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
39 }
40 
41 @end

2. ViewController.h

1 #import <UIKit/UIKit.h>
2 
3 @interface ViewController : UIViewController
4 
5 @property (weak, nonatomic) IBOutlet UIButton *startButton;
6 @property (weak, nonatomic) IBOutlet UITextView *resultsTextView;
7 
8 @end

3. ViewController.m

 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()
 4 
 5 @end
 6 
 7 @implementation ViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     // Do any additional setup after loading the view, typically from a nib.
12 }
13 
14 - (void)didReceiveMemoryWarning {
15     [super didReceiveMemoryWarning];
16     // Dispose of any resources that can be recreated.
17 }
18 
19 - (NSString *)fetchSomethingFromServer {
20     [NSThread sleepForTimeInterval:1];
21     NSURL *url = [NSURL URLWithString:@"http://nycode.sinaapp.com/d.php"];
22     NSError *error = nil;
23     
24     NSURLRequest *request = [NSURLRequest requestWithURL:url];
25     NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
26     NSDictionary *arr = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
27     return [arr objectForKey:@"name"];
28 }
29 
30 - (NSString *)processData:(NSString *)data {
31     [NSThread sleepForTimeInterval:2];
32     return [data uppercaseString];
33 }
34 
35 - (NSString *)calculateFirstResult:(NSString *)data {
36     [NSThread sleepForTimeInterval:3];
37     return [NSString stringWithFormat:@"Number of chars %lu", [data length]];
38 }
39 
40 - (NSString *)calculateSecondResult:(NSString *)data {
41     [NSThread sleepForTimeInterval:4];
42     return [data stringByReplacingOccurrencesOfString:@"E" withString:@"e"];
43 }
44 
45 - (IBAction)doWork:(id)sender {
46     NSDate *startTime = [NSDate date];
47     NSString *fetcheData = [self fetchSomethingFromServer];
48     NSString *processdData = [self processData:fetcheData];
49     NSString *firstResult = [self calculateFirstResult:processdData];
50     NSString *secondResult = [self calculateSecondResult:processdData];
51     NSString *resultsSummary = [NSString stringWithFormat:@"First:[%@]\tSecond:[%@]", firstResult, secondResult];
52     self.resultsTextView.text = resultsSummary;
53     NSDate *endTime = [NSDate date];
54     NSLog(@"Complete in %f seconds", [endTime timeIntervalSinceDate:startTime]);
55 }
56 
57 @end

4. xib文件放一个textview和button并关联输出口和方法

5. 小结

应用程序启动后,程序会运行10秒。

控制台会显示运行的时间间隔

posted @ 2015-04-07 12:34  nycode  阅读(248)  评论(0编辑  收藏  举报