iOS網路: NSURLConnection進行同步下載

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   sp   strong   

  通過 NSURLConnection 的 sendSynchronousRequest:returningResponse:error: 方法建立一 個同步的網路連接。這個方法將會返回一個 NSData 類型的資料    在建立一個同步的網路連接的時候我們需要明白一點,並不是是我們 的這個同步串連一定會堵塞我們的主線程,如果這個同步的串連是建立在主線程上的,那麼 這種情況下是會堵塞我們的主線程的,其他的情況下是不一定會堵塞我們的主線程的。如果你在 GCD 的全域並發隊列上初始化了一個同步的串連,你其實並不會堵塞我們的主線程的。 

 

例子:

- (void)sendSynNetwork{    NSLog(@"Wo are here...");    NSString *urlString = @"http://www.baudu.com";    NSURL *url = [NSURL URLWithString:urlString];    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f];    //同步沒有block    //這樣解決比較方便    NSURLResponse *response = nil;    NSError *error = nil;    NSLog(@"開始同步請求");    NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];    if ([data length] > 0 && error == nil){        NSLog(@"%lu bytes of data was returned.", (unsigned long)[data length]); }    else if ([data length] == 0 && error == nil){        NSLog(@"No data was returned.");    }    else if (error != nil){        NSLog(@"Error happened = %@", error);    }    NSLog(@"We are done.");}

Wo are here...

開始同步請求

8875 bytes of data was returned.

We are done.

 通過上面的代碼我們不難發現,其實我們的主線程是一直堵塞著,也就是說段代碼是一行一行執行的。 下面讓我們看一下,當我們建立一個同步的串連,並且添加到 GCD 的隊列池中去,我們又會發現什麼: 

 

//簡單的同步請求- (void)sendSynNetwork{    NSLog(@"Wo are here...");    NSString *urlString = @"http://www.baudu.com";    NSURL *url = [NSURL URLWithString:urlString];    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f];    dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);    dispatch_async(dispatchQueue, ^{        //同步沒有block        //這樣解決比較方便        NSURLResponse *response = nil;        NSError *error = nil;        NSLog(@"開始同步請求");        NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];        if ([data length] > 0 && error == nil){            NSLog(@"%lu bytes of data was returned.", (unsigned long)[data length]); }        else if ([data length] == 0 && error == nil){            NSLog(@"No data was returned.");        }        else if (error != nil){            NSLog(@"Error happened = %@", error);        }            });   NSLog(@"We are done.");}

Wo are here...

We are done.

開始同步請求

9138 bytes of data was returned.

 

 

 

 

iOS網路: NSURLConnection進行同步下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.