标签:style blog http io color ar 使用 sp for
问题:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSString *fileToDownload = @"http://www.OReilly.com"; [NSThread detachNewThreadSelector:@selector(downloadNewFile:) toTarget:self withObject:fileToDownload]; } - (void)downloadNewFile:(id)paramObject{ @autoreleasepool { NSString *fileURl = (NSString *)paramObject; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:fileURl]]; NSURLResponse *response = nil; NSError *error = nil; NSData *downloadData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if ([downloadData length] > 0) { NSLog(@"下载成功"); }else{ NSLog(@"下载失败"); } } }
二 调用后台线程
问题:
例子:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [self performSelectorInBackground:@selector(firstCounter) withObject:nil]; [self performSelectorInBackground:@selector(secondCounter) withObject:nil]; [self performSelectorInBackground:@selector(thirdCounter) withObject:nil]; return YES; } - (void)firstCounter{ @autoreleasepool{ NSUInteger counter = 0; for (counter = 0;counter < 1000;counter++){ NSLog(@"First Counter = %lu", (unsigned long)counter); } } } - (void) secondCounter{ @autoreleasepool { NSUInteger counter = 0; for (counter = 0;counter < 1000;counter++){ NSLog(@"Second Counter = %lu", (unsigned long)counter); } } } - (void) thirdCounter{ @autoreleasepool { NSUInteger counter = 0; for (counter = 0;counter < 1000;counter++){ NSLog(@"Third Counter = %lu", (unsigned long)counter); } } }
输出为
GCD_Demo5[2507:351136] Third Counter = 0 GCD_Demo5[2507:351136] Third Counter = 1 GCD_Demo5[2507:351136] Third Counter = 2 GCD_Demo5[2507:351136] Third Counter = 3 GCD_Demo5[2507:351134] First Counter = 0 GCD_Demo5[2507:351136] Third Counter = 4 GCD_Demo5[2507:351136] Third Counter = 5
标签:style blog http io color ar 使用 sp for
原文地址:http://www.cnblogs.com/safiri/p/4080840.html