Analysis of IOS networks (asynchronous requests of two networks) and ios
Learning the network, whether it is C/S or B/S, of course, is to send a request to the server and get a response, do you have a request that does not respond, it is not called the network.
This article introduces the realm of Asynchronous Network requests.
* Do not think that the knowledge points I have written are too scattered. I just want to give you a good and clean learning environment. It is easy to explain too much knowledge at a time. *
I will explain it in detail in the code.
The new code.
/// ViewController. m // CX-Asynchronous Network request // Created by ma c on 16/3/17. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // the local server is used here because the network is too slow. NSString * urlString = [NSString stringWithFormat: @ "http: // localhost/tupian.jpg"]; // create a URL Uniform Resource Identifier NSURL * url = [NSURL URLWithString: urlString]; // create a network request/* cachePolicy Cache Policy NSURLRequestUseProtocolCachePolicy = 0, // automatic Cache Policy NSURLRequestReloadIgnoringLocalCacheData = 1, // request the network each time, no matter whether the local cache expires = 2, // If a cache is returned to the cache, the network NSURLRequestReturnCacheDataDontLoad = 3 is loaded. // If a cache is returned, the response is slow. Save, do not load network timeoutInterval request timeout default timeout time is 60 generally set 10-20 s * // This is the most basic method, remember, focus on the following Oh. // NSURLRequest * request = [NSURLRequest requestWithURL: url]; NSURLRequest * request = [NSURLRequest requestWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10]; // to establish an asynchronous connection * requires ios8 and the following * // sendAsynchronousRequest to establish an Asynchronous Network Connection // The queue can be transmitted to the master queue, or the global queue // [NSOperationQueue mainQueue] is transferred to the main queue for direct update // [[NSOperationQueue alloc] init] does not need to be called back to the main thread to update the UI [NSURLConnection sendAsynchronousRequest: request: [NSOperationQueue mainQueue] completionHandler: ^ (NSURLResponse * _ Nullable response, NSData * _ Nullable data, NSError * _ Nullable connectionError) {// reponse indicates the response information. // data indicates the returned data. // connectionError indicates the error message. // print the data NSLog (@ "% @", data) here ); // because of the large amount of binary information, I have intercepted some of it/* 20:12:24. 785 CX-Asynchronous Network request [2982: 155113] <ffd8ffe0 0020.a46 49460001 01000048 00480000 ffe10058 45786966 4154d4d 002a0000 00080002 01120003 00000001 00010000 87690004 00000001 00000026 0003a001 00000000 00030000 109a002 00010001 02eea003 00040000 00010000 02ee0000 00040000 ffed 00010000 usd*/}];} @ end