Try using the Performselectoronmainthread method when iOS updates the UI

Source: Internet
Author: User

When I recently developed a project, I discovered that when I got the data from the Internet, the panic occurred when I used the notification method to refresh the list.

After searching the internet for many articles, it is recommended to use the asynchronous Update method, but still crashes.

Finally, try using the Performselectoronmainthread method to successfully update the TableView.

Let's learn this function now!

The Performselectoronmainthread and Performselectorinbackground of the NSObject class enable simple multithreaded programming techniques

1,-(void) Performselectorinbackground: (SEL) Aselector withobject: (ID) arg

Creates a thread that executes on a child thread, Aselector represents the newly created thread, and ARG is the parameter passed in

2,-(void) Performselectoronmainthread: (SEL) Aselector withobject: (ID) arg waituntildone: (BOOL) wait;

The function of this method is to execute the developed method (code block) in the main thread.

Parameters:

@selector is to define the method that we want to execute.

Withobject:arg defines the parameter object that is passed in when we execute the method. The type is the ID. (We can pass in any parameter)

WAITUNTILDONE:YES Specifies whether the current thread will be blocked until the main thread finishes executing the block of code we have made.

Attention:

1. The Waituntildone:yes parameter is not valid when the current thread is the main path.

2. The method, no return value

3. This method is mainly used to modify the state of the page UI with the main thread.

Try using the Performselectoronmainthread method when iOS updates the UI

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.