IOS development-invalid reloadData (UI update error in sub-thread)

Source: Internet
Author: User

IOS development-invalid reloadData (UI update error in sub-thread)

Today, I encountered a problem while writing a chat tool.

In the registered notification, Every time another user sends a message, the UITableView must be updated.

        cell.textLabel.textColor = [UIColor redColor];

Mark the cell name in red to highlight the new message. (Of course, this is just a demo, and the effect is relatively poor. Normally, the user's profile picture is beating or the number of new messages is displayed ...)


However, I found that the data in UITableVIew was not refreshed after such an update statement was called.

- (void)sessionUpdated:(NSNotification *)notification{      [myTableView reloadData];}

When a breakpoint is set, the numberOfRowsInSection and CellForRow of its Delegate are not called back, so the interface is not updated.

This is confusing.

Then, I manually drag the UI Tableview to reproduce it and find that the data is updated. Normal display, Red.

The logic is correct. Only after reloadData is called, each result is displayed.


Then Baidu learned.

Summary:

Conclusion:

1. UI update is not allowed in sub-threads, but the update result is just a phantom: Because the sub-thread code has been executed, it automatically enters the main thread, the function stack that executes the UI update in the sub-thread. The middle time is very short, so that you may mistakenly think that the UI can be updated by the sub-thread. If the sub-thread is always running, the main thread of the function stack of the UI update in the sub-thread cannot be known, that is, the main thread of the function stack cannot be updated.

2. There are only a few UIS available, because the current environment will be obtained when the thread is opened. If you click a button, the response method of this button is to open up a subthread, you can update the UI of the button in the Child thread in a timely manner, such as changing the title and background image, but this does not make any sense.


Therefore, the updated UI should be executed in the main thread, and the above Code should be changed to this

- (void)sessionUpdated:(NSNotification *)notification{    dispatch_async(dispatch_get_main_queue(), ^{                       [myTableView reloadData];                       });}

Solved the problem.

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.