有没有一个简单的方法(在Cocoa / iOS)将方法调用排队在下一个运行循环中运行一次? [英] Is there a simple way (in Cocoa/iOS) to queue a method call to run once in the next run loop?

查看:271
本文介绍了有没有一个简单的方法(在Cocoa / iOS)将方法调用排队在下一个运行循环中运行一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIView有一个 setNeedsDisplay 方法,可以在同一事件循环中调用多次,因为知道重新绘制工作很快就会发生,而且只有一次。



有这样的行为Cocoa的通用机制吗?一种方式的说法是,选择器按任意次数排队,时间到了,选择器将运行一次并清除队列。



我知道我可以做到这一点与某种状态跟踪在我的目标,或与NSOperationQueue。我只是想知道是否有一个轻松的方法,我错过了。



(当然,答案可能是否。)

解决方案

setNeedsDisplay 不是一个很好的例子,每次你叫它。它只是设置一个标志。但问题是好的。



一个解决方案是使用 NSNotificationCoalescingOnName 。

p>另一个解决方案是建立一个蹦床来做自己的合并。我没有蹦床的真正好的博客参考,但这里是一个例子(
LSTrampoline )。如果你想在一段时间内合并消息,这不是很难建立。我曾经用 forwardInvocation:构建了一个蹦床,类似于:

  - (void)forwardInvocation:(NSInvocation *)invocation {
[invocation setTarget:self.target];
[invocation retainArguments];
[self.timer invalidate];
self.timer = [NSTimer scheduledTimerWithTimeInterval:self.timeout invocation:invocation repees:NO];
}

这实际上会在一段时间内将所有邮件合并到对象消息)。这就是我需要的特定问题。但是你可以扩展这个来跟踪哪些选择器被合并,并检查你的调用,看看他们是否足够匹配。



要让它运行下一个事件循环,只是将超时设置为0。



我对博客的意思保持蹦床。所需先决条件:我的即将出版的图书涵盖第4章和第20章中的蹦床。


UIView has a setNeedsDisplay method that one can call several times within the same event loop, safe in the knowledge that the redrawing work will happen soon, and only the once.

Is there a generic mechanism for this sort of behaviour Cocoa? A way of of saying, "Queue a selector as many times as you like, when it's time, the selector will run once & clear the queue."

I know I could do this with some kind of state tracking in my target, or with an NSOperationQueue. I'm just wondering if there's a lightweight approach I've missed.

(Of course, the answer may be, "No".)

解决方案

setNeedsDisplay is not a good example of what you're describing, since it actually does run every time you call it. It just sets a flag. But the question is good.

One solution is to use NSNotificationQueue with NSNotificationCoalescingOnName.

Another solution is to build a trampoline to do the coalescing yourself. I don't have a really good blog reference for trampolines, but here's an example of one (LSTrampoline). It's not that hard to build this if you want to coalesce the messages over a period of time. I once built a trampoline with a forwardInvocation: similar to this:

- (void)forwardInvocation:(NSInvocation *)invocation {
  [invocation setTarget:self.target];
  [invocation retainArguments];
  [self.timer invalidate];
  self.timer = [NSTimer scheduledTimerWithTimeInterval:self.timeout invocation:invocation repeats:NO];
}

This actually coalesces all messages to the object over the time period (not just matching messages). That's all I needed for the particular problem. But you could expand on this to keep track of which selectors are being coalesced, and check your invocations to see if they match "sufficiently."

To get this to run on the next event loop, just set timeout to 0.

I keep meaning to blog about trampolines. Required shilling: My upcoming book covers trampolines in Chapter 4 and Chapter 20.

这篇关于有没有一个简单的方法(在Cocoa / iOS)将方法调用排队在下一个运行循环中运行一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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