One, non-multithreaded HTTP requests
If you are not using multi-threaded, iOS HTTP access requests, take the login mode as an example:
//This is an incorrect code//after successful login verification, go to the next Viewcontroller-(void) presenttonextview{//to the next interface}//Login Verification-(void) logincheck{//include a post or get request to complete the validation of the data, and verify that the success jumps to the next interface}-(void) showindicator{//show Daisy when logging in}//Click events for the login button-(Ibaction) LOGINBTN: (ID) sender{//the functions performed are:[self showindicator]; [Self Logincheck];}
The result of writing this code is indicator chrysanthemum one to logincheck function execution will be the card master, until the post or GET request is completed before "card to the next interface."
A bad network situation is similar to the freezing state.
Second, the realization of multithreading.
1. Creation of child threads: two methods
The first kind: [Nsthread detachnewthreadselector: @selector (downloadimage:) totarget:self Withobject:kurl];
The second kind: nsthread *thread = [[Nsthread alloc]initwithtarget:self selector: @selector (downloadimage:) Object:kurl];
[Thread start];
Difference: The first kind of direct convenience, the second before the start can be configured for its stack parameters, more flexibility.
2. Invoke the main thread in the sub-thread to do the work:
[Self performselectoronmainthread: @selector (name of function) Withobject:nil Waituntildone:yes];
If the Waituntildone parameter is yes, then the thread will be blocked until selector runs out.
3. Simple code example one: Login
//This is a multithreaded implementation of the login put UI card dead code//after successful login verification, go to the next Viewcontroller-(void) presenttonextview{//to the next interface}//Login Verification-(void) logincheck{//include a post or get request to complete the validation of the data, and verify that the success jumps to the next interface if(account password match) {//call the main thread to jump to the next interface[Self performselectoronmainthread: @selector (Presenttonextview) Withobject:nil Waituntildone:yes]; }}-(void) showindicator{//show Daisy when logging in}//Click events for the login button-(Ibaction) LOGINBTN: (ID) sender{//the functions performed are:[self showindicator]; //open new threads, execute functions that require networking time-consuming Logincheck[Nsthread detachnewthreadselector: @selector (Logincheck) totarget:self Withobject:nil];}
4. Code example two: Display a picture on the web, source code:
DownloadViewController.h
#import <UIKit/UIKit.h>#import"FileConnection.h"@ Interface downloadviewcontroller:uiviewcontroller@end
Downloadviewcontroller.m
#import "DownloadViewController.h"#defineKurl @ "http://b.hiphotos.baidu.com/image/pic/item/32fa828ba61ea8d37ccb6e0e950a304e241f58ca.jpg "@interfaceDownloadviewcontroller () @property Uiimageview*ImageView;@endUiactivityindicatorview*Activityindicatorview;@implementationDownloadviewcontroller-(void) Downloadimage: (NSString *) url{NSData*data =[[NSData alloc] Initwithcontentsofurl:[nsurl Urlwithstring:url]]; UIImage*image =[[UIImage Alloc]initwithdata:data]; if(Image = =Nil) {NSLog (@"No pictures"); }Else{NSLog (@"Refresh Picture"); [Activityindicatorview stopanimating];//Stop//notify the main thread to do things[Self performselectoronmainthread: @selector (updateUI:) withobject:image Waituntildone:no]; //if the Waituntildone parameter is yes, then the thread will be blocked until selector runs out. }}-(void) UpdateUI: (uiimage*) image{Self.imageView.image=image; //sleep (5);}-(void) showindicatior{Activityindicatorview= [[Uiactivityindicatorview alloc] Initwithframe:cgrectmake ( -, -,30.0,30.0)]; Activityindicatorview.activityindicatorviewstyle=Uiactivityindicatorviewstylegray; [Self.view Addsubview:activityindicatorview]; [Activityindicatorview startanimating];//Start}- (void) viewdidload{[Super Viewdidload]; Self.imageview=[[uiimageview Alloc] Initwithframe:cgrectmake (Ten, -, -, -)]; [Self.view AddSubview:self.imageView]; //Show Chrysanthemums[self showindicatior]; //open up a new thread 2 ways[Nsthread detachnewthreadselector: @selector (downloadimage:) totarget:self Withobject:kurl]; //nsthread *thread = [[Nsthread alloc]initwithtarget:self selector: @selector (downloadimage:) Object:kurl]; //[Thread start];}- (void) didreceivememorywarning{[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end