IPhoneDevelopmentMultithreadingUsage and precautions: This article describes how to use multithreading in the iPhone SDK and precautions. Although most PC applications currently supportMultithreading/Multi-task development method, but inIPhoneOn, Apple is not recommendedMultithreadingProgramming Method.
HoweverMultithreadingAfter all, programming is a development trend and is said to be coming soon.IPhoneOS4 will be fully supportedMultithreading. So MasterMultithreadingProgramming method, in some cases can be dug outIPhoneMore potential.
Start with an example
Start with a routine. For more information about the code, see here. You can download other routines.
For the control model of multi-threaded programs, see here. Generally, the manager/worker model is used. Here, we use NSThread in the iPhone SDK to implement it.
First, create a new View-based application project named "TutorialProject ". As shown in the interface, use UILabel to implement the two parts (Thread Part and Test Part). The Thread Part contains a UIProgressView and a UIButton. The Test Part contains a value and a UISlider,
Next, create IBOutlets for each UI control in the TutorialProjectViewController. h file.
- @interface TutorialProjectViewController : UIViewController {
- // ------ Tutorial code starts here ------
- // Thread part
- IBOutlet UILabel *threadValueLabel;
- IBOutlet UIProgressView *threadProgressView;
- IBOutlet UIButton *threadStartButton;
- // Test part
- IBOutlet UILabel *testValueLabel;
- // ------ Tutorial code ends here ------
- }
At the same time, you also need to create the property of the outlets variable.
- @property (nonatomic, retain) IBOutlet UILabel *threadValueLabel;
- @property (nonatomic, retain) IBOutlet UIProgressView *threadProgressView;
- @property (nonatomic, retain) IBOutlet UIProgressView *threadStartButton;
- @property (nonatomic, retain) IBOutlet UILabel *testValueLabel;
Next, define the action function when the button is pressed and the variable function of slider.
- - (IBAction) startThreadButtonPressed:(UIButton *)sender;
- - (IBAction) testValueSliderChanged:(UISlider *)sender;
Then in the TutorialProjectViewController. m file synthesize outlets, and in the file to implement dealloc release resources.
- @synthesize threadValueLabel, threadProgressView, testValueLabel, threadStartButton;
- ...
- - (void)dealloc {
- // ------ Tutorial code starts here ------
- [threadValueLabel release];
- [threadProgressView release];
- [threadStartButton release];
- [testValueLabel release];
- // ------ Tutorial code ends here ------
- [super dealloc];
- }
Start nowThreadFirst, when the thread button is pressed, create a newThread.
- - (IBAction) startThreadButtonPressed:(UIButton *)sender {
- threadStartButton.hidden = YES;
- threadValueLabel.text = @"0";
- threadProgressView.progress = 0.0;
- [NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:self withObject:nil];
- }
After the button is pressed, hide the button to prevent multiple threads from being created. Then initialize the display value and progress bar, and create a new thread. The thread function is startTheBackgroundJob.
The specific startTheBackgroundJob function is defined as follows.
- -(Void) startTheBackgroundJob {
- NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init];
- // Pause the thread for 3 seconds after it starts. Here, we only demonstrate the method for pausing the thread. You do not have to do this)
- [NSThread sleepForTimeInterval: 3];
- [Self defined mselecw.mainthread: @ selector (makeMyProgressBarMoving) withObject: nil waitUntilDone: NO];
- [Pool release];
- }
In row 1st, An NSAID utoreleasepool object is created to manageThreadAutomatically released object Resources. Here, the NSAID utoreleasepool isThreadReleased upon exit. This complies with the general rules of the Cocoa GUI application.