[Reprint]ios Development Why the update UI should be placed in the main thread?

Source: Internet
Author: User

There are 2 reasons:

1, in the child thread is not the UI update, and can update the result is just a phantom: because the child thread code execution is complete, and automatically into the main thread, executing the UI updates in the child thread function stack, the middle of the time is very short, let everyone mistakenly think that the sub-thread can update the UI. If a child thread is running, the main thread of the function stack in the UI update in the child thread is not known, and cannot be updated

2, only a very small number of UI can, because the thread will get the current environment, such as click on a button, this button response is to open a sub-thread, in the child thread UI update is timely, such as the title, change the background, but it doesn't make any sense

1, the program began to run into the main thread

2, processing some data too time-consuming, affect user interaction, can open up sub-threading processing, and then notify the main thread to update the interface

Test code:

Open a multi-threaded, direct UI update on the thread:

-(void) Testuirefresh: (UIButton *) button{

[Nsthread detachnewthreadselector: @selector (begintest) totarget:self Withobject:nil];

}

-(void) Begintest {
NSLog (@ "Current thread%@", [Nsthread CurrentThread]);

NSLog (@ "main thread%@", [Nsthread Mainthread]);

This button is the button that responds to the Testuirefresh

[Self.button settitle:@ "AAA" forstate:0];

}

2012-11-15 12:14:02.147 testproj[2455:1b07] Current thread {name = (null), num = 3}

2012-11-15 12:14:02.147 testproj[2455:1b07] Main thread {name = (null), num = 1}

Result: It is currently in a child thread, but the UI is updated immediately??

Results analysis: Everyone says UI updates are done in the main thread and the results above are explained

Assume: If UI updates are made in a child thread, the program automatically goes to the main thread for the specified UI update when the child thread finishes running!

Question: What if the child thread is not over?

To join in a split thread:
-(void) begintest{
NSLog (@ "Current thread%@", [Nsthread CurrentThread]);

NSLog (@ "main thread%@", [Nsthread Mainthread]);

This button is the button that responds to the Testuirefresh

[Self.button settitle:@ "AAA" forstate:0];

[Nsthread sleepfortimeinterval:4.0];

}

Results:

Self.button's title was updated immediately.

Result Analysis: Is the above hypothesis not tenable?

Problem: This time Add a button to the split thread

-(void) begintest{

NSLog (@ "Current thread%@", [Nsthread CurrentThread]);

NSLog (@ "main thread%@", [Nsthread Mainthread]);

      //the button for the    response Testuirefresh
       [self.button settitle:@ "AAA" forstate:0];
      uibutton *backbutton = [UIButton buttonwithtype:uibuttontypecustom];
      [backbutton settitle:@ "Test Runloop" forstate:0];
      [backbutton Settitlecolor:[uicolor Redcolor] forState:0];
      backbutton.frame = CGRectMake (100, 200, 100, 50);
      [backbutton addtarget:self Action: @selector (Testrunloop)      forControlEvents:UIControlEventTouchUpInside];
      [self.window Addsubview:backbutton];
      [nsthread sleepfortimeinterval:4.0];

}
 
Result: [Self.button settitle:@ "AAA" forstate:0]; response immediately, but the added button waits until the thread ends to draw
 
Analysis: In a child thread: If you want to update another UI, you must wait until the child thread finishes running, and the UI update to the button that responds to the user's click is timely! Whether he's updating in the main thread or in a sub-thread, it doesn't make much sense, because all other UI updates in a child thread wait until the end of the child thread's life cycle.
 
 
 
Conclusion:
 
1, UI update is not possible in child threads, and the result can be updated is just a phantom: because the child thread code executes, and automatically enters the main thread, A function stack that executes UI updates in a child thread, which is very short in time, makes it a mistake to think that a thread can update the UI. If the child thread is running continuously, the main thread of the function stack in the UI update in the child thread is not known, that is, the
  cannot be updated;
2, only a very small number of UI can, because the thread will get the current environment, such as click on a button, this button response is to open a sub-thread, UI updates to the button in a child thread can be timely, such as changing the title and changing the background, but that doesn't make any sense

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.