将选择器放入运行循环 - 是[NSObject performSelector:withObject:afterDelay:]要走的路? [英] Enqueue a selector to the run loop - is [NSObject performSelector:withObject:afterDelay:] the way to go?

查看:173
本文介绍了将选择器放入运行循环 - 是[NSObject performSelector:withObject:afterDelay:]要走的路?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在当前方法通过并且UI已更新后,执行一个方法。为此,我现在使用 [object performSelector:@selector(someSelector)withObject:someObject afterDelay:0.0] 。根据 Apple的文档,此创建一个NSTimer,然后触发并将选择器附加到当前的NSRunLoop。但我不认为这非常优雅。有没有一个简单的方法来直接将选择器入队到当前的运行循环,而没有Cocoa创建一个计时器等。

I'd like to have a method be executed after the current method has passed and the UI has been updated. For that purpose, I'm using [object performSelector:@selector(someSelector) withObject:someObject afterDelay:0.0] right now. According to Apple's documentation, this creates a NSTimer which will then trigger and append the selector to the current NSRunLoop. But I don't consider this very elegant. Is there an easy way to directly enqueue the selector to the current run loop, without having Cocoa create a Timer etc.?

performSelectorOnMainThread:withObject :waitUntilDone:(如果我在主线程)或 performSelector:onThread:withObject:waitUntilDone: with waitUntilDone

Would performSelectorOnMainThread:withObject:waitUntilDone: (if I'm on the main thread) or performSelector:onThread:withObject:waitUntilDone: with waitUntilDone:NO do what I want with less overhead?

提前欢呼并提前感谢

MrMage

推荐答案

Cocoa是事件驱动的。你不在当前运行循环中排队一个选择器。简单来说:发送到应用程序的事件(用户输入,定时器,网络活动...)导致运行循环运行,这导致在循环的运行中发生事情。当然有细节,但这是最基本的行为。

Cocoa is event-driven. You don't "enqueue a selector within the current run loop". To put it simplistically: An event sent to the application (user input, a timer, network activity ...) causes the run loop to run, which causes things to happen in that run of the loop. There are of course "details", but this is the most basic behavior.

如果你想推迟一些选择器到当前运行循环结束,调用它最后,或要求它在一个(非常接近)即将到来的循环运行。 -performSelector:...方法正确的方法。他们创建一个定时器,导致发生事件的事件。

If you want to put off performing some selector to the end of the current run loop, call it last, or ask it to be run on a (very near) upcoming run of the loop. The -performSelector:... methods are the correct way to do this. They create a timer which results in an event which causes things to happen.

有关详细信息,请参阅可可事件处理指南

For more information, see the Cocoa Event-Handling Guide.

这篇关于将选择器放入运行循环 - 是[NSObject performSelector:withObject:afterDelay:]要走的路?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆