Instance Method インスタンスメソッド

performSelector(onMainThread:with:waitUntilDone:modes:)

Invokes a method of the receiver on the main thread using the specified modes. レシーバのメソッドをメインスレッド上で指定モードを使って発動します。

Declaration 宣言

func performSelector(onMainThread aSelector: Selector, 
                with arg: Any?, 
       waitUntilDone wait: Bool, 
               modes array: [String]?)

Parameters パラメータ

aSelector

A Selector that identifies the method to invoke. The method should not have a significant return value and should take a single argument of type id, or no arguments.

arg

The argument to pass to the method when it is invoked. メソッドに、それが発動されるときに渡される引数。 Pass nil if the method does not take an argument.

wait

A Boolean that specifies whether the current thread blocks until after the specified selector is performed on the receiver on the main thread. メインスレッドのレシーバで指定されたセレクタが実行される後まで現在のスレッドを封鎖するかどうかを指定するブール。 Specify true to block this thread; otherwise, specify false to have this method return immediately.

If the current thread is also the main thread, and you pass true, the message is performed immediately, otherwise the perform is queued to run the next time through the run loop.

array

An array of strings that identifies the modes in which it is permissible to perform the specified selector. This array must contain at least one string. 文字列からなる配列、それは指定されたセレクタの実行が許可されるモードを識別します。この配列は、少なくとも1つの文字列を含まなければなりません。 If you specify nil or an empty array for this parameter, this method returns without performing the specified selector. For information about run loop modes, see Run Loops in Threading Programming Guide.

Discussion 解説

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 in this case is a method of the current object that you want to execute on the thread. この場合におけるメッセージは、現在のオブジェクトのメソッドです、それはあなたがこのスレッド上で実行するのを望むものです。

This method queues the message on the run loop of the main thread using the run loop modes specified in the array parameter. As part of its normal run loop processing, the main thread dequeues the message (assuming it is running in one of the specified modes) and invokes the desired method. Multiple calls to this method from the same thread cause the corresponding selectors to be queued and performed in the same same order in which the calls were made, assuming the associated run loop modes for each selector are the same. If you specify different modes for each selector, any selectors whose associated mode does not match the current run loop mode are skipped until the run loop subsequently executes in that mode. それの標準的な実行ループ処理の一部として、メインスレッドはメッセージを(それが指定されたモードの1つで実行されていると仮定して)デキューして、要望されたメソッドを発動します。このメソッドの同じスレッドからの複数の呼び出しは、該当しているセレクタがデキューされて、それらの呼び出しがされたのと同じ順序で実行されるようにします、それぞれのセレクタに関して実行ループモードが同じであると仮定します。あなたが異なるモードをそれぞれのセレクタに指定するならば、それと結びつけられたモードが現在の実行ループモードと一致しないあらゆるセレクタは、実行ループが後でそのモードで実行されるまでスキップされます。

You cannot cancel messages queued using this method. あなたは、このメソッドを使ってキューされたメッセージをキャンセルできません。 If you want the option of canceling a message on the current thread, you must use either the perform(_:with:afterDelay:) or perform(_:with:afterDelay:inModes:) method.

Special Considerations 特別な注意事項

This method registers with the runloop of its current context, and depends on that runloop being run on a regular basis to perform correctly. One common context where you might call this method and end up registering with a runloop that is not automatically run on a regular basis is when being invoked by a dispatch queue. このメソッドは、それの現在の文脈の実行ループに登録します、そしてその実行ループが通常の原則上で動かされて正しく成し遂げられることをあてにします。あなたがこのメソッドを呼び出して自動的には通常の原則上で動かされない実行ループに登録することに終わるかもしれないよくある文脈は、ディスパッチキューによって発動されている時です。 If you need this type of functionality when running on a dispatch queue, you should use dispatch_after(_:_:_:) and related methods to get the behavior you want.

See Also 参照

Sending Messages メッセージ送信