October 2013
Intermediate to advanced
1053 pages
28h 7m
English
You want to know an easy way to create threads without having to deal with threads directly.
Use the performSelectorInBackground:withObject:
instance method of NSObject:
-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{[selfperformSelectorInBackground:@selector(firstCounter)withObject:nil];[selfperformSelectorInBackground:@selector(secondCounter)withObject:nil];[selfperformSelectorInBackground:@selector(thirdCounter)withObject:nil];self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];self.window.backgroundColor=[UIColorwhiteColor];[self.windowmakeKeyAndVisible];returnYES;}
The counter methods are implemented in this way:
-(void)firstCounter{@autoreleasepool{NSUIntegercounter=0;for(counter=0;counter<1000;counter++){NSLog(@"First Counter = %lu",(unsignedlong)counter);}}}-(void)secondCounter{@autoreleasepool{NSUIntegercounter=0;for(counter=0;counter<1000;counter++){NSLog(@"Second Counter = %lu",(unsignedlong)counter);}}}-(void)thirdCounter{@autoreleasepool{NSUIntegercounter=0;for(counter=0;counter<1000;counter++){NSLog(@"Third Counter = %lu",(unsignedlong)counter);}}}
The performSelectorInBackground:withObject: method creates a new thread in the background for us. This is equivalent to creating a new thread for the selectors. The most important thing we have ...
Read now
Unlock full access