iOS Dev-reloaddata Invalid (child thread update UI error)

Source: Internet
Author: User

There was a problem with writing a chat tool today.

In the notification of registration, every time a message is sent to another user, UITableView will be re-updated

        Cell.textLabel.textColor = [Uicolor Redcolor];

Highlight the new message by using red to mark the cell name.  (Of course, this is just a demo, the effect is slag.) The normal situation should be the user's avatar beating or showing the number of new message bars ...)


But I found that after invoking such an update statement, the data inside the UITableView was not refreshed.

-(void) sessionupdated: (nsnotification *) notification{      [Mytableview reloaddata];}

Set the next breakpoint, found that there is no callback to its delegate numberofrowsinsection and Cellforrow, so the interface is not updated.

That's a puzzle.

Then I manually dragged the UI Tableview to redraw it and found the data updated. Normal display, turn red.

The explanation logic is correct. Just call the Reloaddata after each effect.


After that, Baidu study.

Summarized as follows:

Conclusion:

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


Therefore, the update UI is executed in the main thread, and the above code is changed to

-(void) sessionupdated: (nsnotification *) notification{    Dispatch_async (Dispatch_get_main_queue (), ^{                       [ Mytableview reloaddata];                       });

Resolved the problem.

iOS Dev-reloaddata Invalid (child thread update UI error)

Related Article

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.