IOS: Learning notes, Performselectoronmainthread

Source: Internet
Author: User

-(void) Performselectoronmainthread: (SEL) Aselector withobject: (ID) arg waituntildone: (BOOL) wait
PerformSelectorOnMainThread:withObject:waitUntilDone:

Method of calling the receiver in the main thread based on the default mode
Invokes a method of the receiver on the main thread using the default mode.

Parameter Parameters
Aselector

A selector that specifies the method to invoke. The method should not have a definite return value and at most only one parameter of type ID, or no parameter.
A selector that identifies the method to invoke. The method should not has a significant return value and should take a single argument of type ID, or no arguments.

Arg
The arguments that need to be passed to the calling method. If no parameters are required, pass in nil
The argument to pass to the method when it is invoked. Pass Nil If the method does not take an argument.

Wait
A Boolean value that specifies whether to block the current thread until the specified selector finishes executing in the main thread. Selecting Yes will block the thread, and the method will return immediately if no is selected.
A Boolean that specifies whether the current thread blocks until after the specified selector are performed on the receiver On the main thread. Specify YES to block this thread; Otherwise, specify NO to has this method return immediately.
If current thread is also the main thread, select Yes, the message will be dispatched and processed immediately.
If The current thread was also the main thread, and you specify YES to this parameter, the message is delivered and proces Sed immediately.

Discussion Discussion

You can use this method to send messages to the main thread. The main thread contains the primary loop of the application (this is where NSApplication receives events). In this case, the message corresponds to the method of the current object that you want to execute on the thread.
You can use this method to deliver messages to the main thread of your application. The main thread encompasses the application ' s main run loop, and is where the NSApplication object receives events. The message is a method of the current object, which you want to execute on the thread.

This method queues the message in the run loop of the main thread using the common run loop modes-that is, the modes a Ssociated with the Nsrunloopcommonmodes constant. As part of its normal run loop processing, the main thread dequeues the message (assuming it's running in one of the Comm On run loop modes) and invokes the desired method. Multiple calls to this method from the same thread cause the corresponding selectors to being queued and performed in the SAM e same order in which the calls were made.

You cannot cancel the addition of this method The message for the queue. If you want to cancel a message for the current thread, you must use
PerformSelector:withObject:afterDelay: or  
PerformSelector:withObject:afterDelay : Inmodes: Method.
You cannot cancel messages queued using the This method. If you want the option of canceling a message on the current thread, you must use either the 
Performselector:with object:afterdelay:or 
PerformSelector:withObject:afterDelay:inModes:method.

special note Meaning
Special Considerations

This method registers the current context in Runloop and relies on Runloop to run periodically to ensure proper execution. (????? Not translated here, please help) . If you need this type of functionality when running a dispatch queue, you should use Dispatch_after and related methods to get the behavior you want.
This method registers with the runloop of it current context, and depends on that Runloop being run on a regular basi s to perform correctly. One common context where you might call the This method and end up registering with a run Loop that was not automatically run on a regular basis was when being invoked by a dispatch queue . If you need this type of functionality when running on a dispatch the queue, you should use Dispatch_after and related methods To get the behavior want.

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.