IPhone development-Multithreading

Source: Internet
Author: User

 

Examples show how to create a subthread.

The nsthread class is used. Here we use detachnewtheadselector: totagaet: withobject to create a thread.

Function setupthread :( nsarray *) userinfor. Use userinfor to upload the required data to the thread.

Function Definition:

-(Void) Setupthread :( nsarray *) userinfor {

[NsthreadDetachnewthreadselector: @ selector (threadfunc :) totarget: Self withobject :( ID) userinfor];

//This function is the same as the pthread_create function, but the pthread_create function cannot be used directly during XIB interaction, because pthread_create calls a C function (equivalent to this selector ), the control of XIB cannot be called in the C function. This is my problem and there may be other solutions.

}

-(Void) Threadfunc :( ID) userinfor {

NSAID utoreleasepool* Pool = [[NSAID utoreleasepoolAlloc] init];

//.... The processing that needs to be done.

// Return immediately after the thread ends

[Self defined mselecw.mainthread: @ selector (endthread) withobject: Nil waituntildone: No];

[Pool release];

}

Performselectoronmainthread notifies the main thread to execute the function endthread. You can also use javasmselector: onthread: withobject: waituntil to notify a thread of processing after the execution thread ends.

Do not refresh the interface within the thread. If you need to refresh the interface, call out the methods in the main thread to refresh through performselecdomainmainthread. //This sentence is too critical .. Finally found the Organization

 

For example, start a thread to download images:

// Start the thread

[Nsthread detachnewthreadselector: @ selector (downloadimage :) totarget: Self withobject: url];

// Thread Functions

//What are the functions of nsurl?

-(Void) downloadimage :( Nsstring *) URL {

_ Subthreed = [ Nsthread Currentthread];

Self. uploadpool = [[ NSAID utoreleasepool Alloc] init];
Self. characterbuffer = [ Nsmutabledata Data];
Done = no;
[[Nsurlcache sharedurlcache] removeallcachedresponses];

Nsmutableurlrequest * therequest = [ Nsmutableurlrequest Requestwithurl :[ Nsurl Urlwithstring: url];

Self. Connection = [[ Nsurlconnection Alloc] initwithrequest: therequest delegate: Self];
[Self check mselecw.mainthread: @ selector (httpconnectstart) withobject: Nil waituntildone: No];
If (connection! = Nil ){
Do {
[[Nsunloop currentrunloop] runmode: nsdefaultrunloopmode beforedate: [nsdate distantfuture]; // This nsunloop seems very useful.
} While (! Done );
}

Self. Photo = [uiimage imagewithdata: characterbuffer];

 // Download is complete. Refresh (Inter-thread Communication)
[Self defined mselecw.mainthread: @ selector (fillphoto) withobject: Nil waituntildone: No];

// Release resources used only in this thread.
Self. Connection = nil;
[Uploadpool release];
Self. uploadpool = nil;

_ Subthreed = nil;
}

 

# Pragma mark nsurlconnection delegate Methods

/*
Disable caching so that each time we run this app we are starting with a clean slate. You may not want to do this in your application.
*/
-(Nscachedurlresponse *) connection :( nsurlconnection *) connection willcacheresponse :( nscachedurlresponse *) cachedresponse {

Return nil;
}

// Forward errors to the delegate.
-(Void) connection :( nsurlconnection *) connection didfailwitherror :( nserror *) error {
Done = yes;
[Self check mselecw.mainthread: @ selector (httpconnectend) withobject: Nil waituntildone: No];
[Characterbuffer setlength: 0];

}

// Called when a chunk of data has been downloaded.
-(Void) connection :( nsurlconnection *) connection didreceivedata :( nsdata *) Data {
// Process the downloaded chunk of data.
 
[Characterbuffer appenddata: Data];

}

-(Void) connectiondidfinishloading :( nsurlconnection *) connection {

[Self check mselecw.mainthread: @ selector (httpconnectend) withobject: Nil waituntildone: No];
// Set the condition which ends the run loop.
Done = yes;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.