1.在主線程發(fā)起網(wǎng)絡(luò)請求,使用[NSURLConnection connectionWithRequest:request delegate:self]設(shè)置代理方法,代理方法默認(rèn)是在主線程中調(diào)用的
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]];
//設(shè)置代理
//代理方法:默認(rèn)是在主線程中調(diào)用的
NSURLConnection *connect = [NSURLConnection connectionWithRequest:request delegate:self];
//設(shè)置代理方法在哪個線程中調(diào)用
//[NSOperationQueue alloc]init]] 開子線程
//[NSOperationQueue mainQueue] 不能這樣設(shè)置
// 設(shè)置代理在子線程執(zhí)行
[connect setDelegateQueue:[[NSOperationQueue alloc]init]];
//[connect setDelegateQueue:[NSOperationQueue mainQueue]];
NSLog(@"-------");
2.在主線程中發(fā)起網(wǎng)絡(luò)請求,使用[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:NO]設(shè)置代理,代理方法默認(rèn)是在主線程中調(diào)用的
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]];
//設(shè)置代理
//代理方法:默認(rèn)是在主線程中調(diào)用的
NSURLConnection *connect = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:NO];
// 設(shè)置代理在子線程執(zhí)行
[connect setDelegateQueue:[[NSOperationQueue alloc]init]];
//開始發(fā)送請求
[connect start];
NSLog(@"-------");
3.發(fā)起網(wǎng)絡(luò)請求在子線程執(zhí)行,使用使用[NSURLConnection connectionWithRequest:request delegate:self]設(shè)置代理方法,代理方法默認(rèn)是在子線程中調(diào)用的
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]];
//設(shè)置代理
//代理方法:默認(rèn)是在子線程中調(diào)用的!!
//該方法內(nèi)部其實(shí)會將connect對象作為一個source添加到當(dāng)前的runloop中,指定運(yùn)行模式為默認(rèn)
NSURLConnection *connect = [NSURLConnection connectionWithRequest:request delegate:self];
//設(shè)置代理方法在哪個線程中調(diào)用,在這里可設(shè)置為主線程,也可以設(shè)置為子線程
[connect setDelegateQueue:[[NSOperationQueue alloc]init]];
//[[NSRunLoop currentRunLoop] runMode:UITrackingRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:1000]];
[[NSRunLoop currentRunLoop]run];
NSLog(@"---%@----",[NSThread currentThread]);
});
4.在子線程中發(fā)起網(wǎng)絡(luò)請求,使用[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:NO]設(shè)置代理,同時還需要設(shè)置代理方法執(zhí)行所在的線程[connect setDelegateQueue:[NSOperationQueue mainQueue]]
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]];
//設(shè)置代理
//代理方法:默認(rèn)是在主線程中調(diào)用的
NSURLConnection *connect = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:NO];
//設(shè)置代理方法在哪個線程中調(diào)用,在這里可設(shè)置為主線程,也可以設(shè)置為子線程,但是,這里必須要設(shè)置,否則不會走代理方法
[connect setDelegateQueue:[NSOperationQueue mainQueue]];
//開始發(fā)送請求
//如如果connect對象沒有添加到runloop中,那么該方法內(nèi)部會自動的添加到runloop
//注意:如果當(dāng)前的runloop沒有開啟,那么該方法內(nèi)部會自動獲得當(dāng)前線程對應(yīng)的runloop對象并且開啟
[connect start];
NSLog(@"---%@----",[NSThread currentThread]);
});