From MobileMasteryApp/threading/01
long task that blocks UI
In this example, we use the Activity Indicator to indicate the UI response. As you can see, when we start the loop to run through 40,000.
1 |
[self.myActivityIndicator startAnimating]; |
to make it start animating. However, on the emulator, you won’t see it moving because the current main thread will process the loop count. When it finishes, then it stops the animating. However, visually, you won’t see the indicator move at all.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#import "ViewController.h" @implementation ViewController @synthesize myButton, myActivityIndicator; -(void) bigTask{ for(int i=0;i<40000;i++){ NSString *newString = [NSString stringWithFormat:@"i = %i", i]; NSLog(@"%@", newString); } [self.myActivityIndicator stopAnimating]; } //do task without using a new thread (watch UI to see how this works) //blocks UI !!!!!!!!!! -(void)bigTaskAction{ [self.myActivityIndicator startAnimating]; [self bigTask]; } - (void)viewDidLoad{ [super viewDidLoad]; //Create button self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; self.myButton.frame = CGRectMake(20, 403, 280, 37); [self.myButton addTarget:self action:@selector(bigTaskAction) forControlEvents:UIControlEventTouchUpInside]; [self.myButton setTitle:@"Do Long Task" forState:UIControlStateNormal]; [self.view addSubview:self.myButton]; //Create activity indicator self.myActivityIndicator = [[UIActivityIndicatorView alloc] init]; self.myActivityIndicator.frame = CGRectMake(142, 211, 37, 37); self.myActivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge; self.myActivityIndicator.hidesWhenStopped = NO; [self.view addSubview:self.myActivityIndicator]; } @end |
Non blocking task
In this example, we detach a thread to do our long process for us. Thus, this spares the main thread of extra work. The main thread’s task is to update the UI and do little processing as possible. Leave the heavy lifting to separate threads.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#import "ViewController.h" @implementation ViewController @synthesize myButton, myActivityIndicator; -(void) bigTask{ for(int i=0;i<40000;i++){ NSString *newString = [NSString stringWithFormat:@"i = %i", i]; NSLog(@"%@", newString); } [self.myActivityIndicator stopAnimating]; } //does not block UI //do task by detaching a new thread (watch UI to see how this works) -(void)bigTaskAction{ [self.myActivityIndicator startAnimating]; [NSThread detachNewThreadSelector:@selector(bigTask) toTarget:self withObject:nil]; } - (void)viewDidLoad{ [super viewDidLoad]; //Create button self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; self.myButton.frame = CGRectMake(20, 403, 280, 37); [self.myButton addTarget:self action:@selector(bigTaskAction) forControlEvents:UIControlEventTouchUpInside]; [self.myButton setTitle:@"Do Long Task" forState:UIControlStateNormal]; [self.view addSubview:self.myButton]; //Create activity indicator self.myActivityIndicator = [[UIActivityIndicatorView alloc] init]; self.myActivityIndicator.frame = CGRectMake(142, 211, 37, 37); self.myActivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge; self.myActivityIndicator.hidesWhenStopped = NO; [self.view addSubview:self.myActivityIndicator]; } @end |