当 runloop 事件处理完成后,我该怎么做?

发布于 2024-09-05 01:24:26 字数 410 浏览 1 评论 0原文

我的 Cocoa 应用程序中有一些处理,有时最终会调用数据层次结构来完成一系列工作作为事件的结果。每个小块都会创造和破坏一些资源。我不希望这些资源在大多数时间都存在,但我想找到一种聪明的方法在所有工作之前创建它们并在最后杀死它们。

如果无法从“父级”或其他地方全局使用这些缓冲区等,有没有办法在某些代码中本地知道事件循环运行何时结束?然后,如果它们不存在,我可以创建它们,并保留它们直到运行循环结束,并在该时间之前将它们重新用于任何后续调用。

编辑:我不是在寻找有关如何重组我的代码的建议,无论如何我都可以这样做。这个问题给我带来了如何知道运行循环何时完成的问题。如果我用 Javascript 编写,我不知道,我会使用 0 的 setTimeout 来完成结束事件清理。我想间隔为零的 NSTimer 也可以实现这一点,但想知道是否有更干净的东西。

谢谢。

I have some processing in my Cocoa app that sometimes ends up calling through a hierarchy of data to do a bunch of work as the result of an event. Each small piece creates and destroys some resources. I don't want those resources around most of the time, but I would like to find a smart way of creating them before all the work and killing them at the end.

Short of making those buffers etc available globally from the "parent" or elsewhere, is there a way to know locally in some code when an event loop run has ended? Then I could create them if they're not there, and keep them until the run loop ends, reusing them for any subsequent calls before that time.

EDIT: I'm not looking for suggestions on how to restructure my code, which I may do anyways. This issue just brought up the question for me of how to know when the runloop is done. If I were writing in, I dunno, Javascript, I'd use a setTimeout with zero to accomplish end-event cleanup. I suppose an NSTimer with an interval of zero might accomplish this too, but wondering if there's something cleaner.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼波传意 2024-09-12 01:24:26

既然你说“Cocoa”和“NSRunLoop”,我就假设你在Mac OS X上。只要你在Snow Leopard上,你就可以使用Grand Central Dispatch非常优雅地解决这类问题。

如果在 Leopard 或更高版本(或 iPhone)上,您可以使用 NSOperations 执行相同的操作(代码稍多)。

所有这些都在 并发编程指南

即使您的算法不是设计为在主线程之外执行(在主事件循环之外),您仍然可以解决“稍后”调度内容的问题,以便通过主队列串行执行。

Since you said "Cocoa" and "NSRunLoop", I'm going to assume you are on Mac OS X. As long as you are on Snow Leopard, you can use Grand Central Dispatch to solve this kind of problem very elegantly.

If on Leopard or later (or iPhone, for that matter), you can use NSOperations to do the same (with slightly more code).

All of this is discussed in the Concurrency Programming Guide.

Even if your algorithm isn't designed to be executed off of the Main thread -- outside of the Main event loop -- you could still solve the problem of scheduling stuff for "later", to be executed serially, via the main queue.

獨角戲 2024-09-12 01:24:26

我对 Cocoa 编程还比较陌生,但这不是可以通过类级变量和该变量的 getter 轻松完成吗?

假设您的代码是这样的,用伪代码表示:

bool completed = false;

void chi
  if completed = false
    create foo;
    create bar;

   completed = true 
  end if

  while looping
     ...
  loop

  completed = false;
  destroy foo
  destroy bar

在程序的其他部分中,检查“completed”的值以查看是否创建了所述对象?

编辑:我刚刚重读了你的问题并进行了相应的编辑。如果这是一个多线程应用程序,您还需要确保您的对象是线程安全的。

I'm relatively new to programming in Cocoa, but wouldn't this just be easily accomplished by a class-level variable with a getter to that variable?

Let's say your code is like this, in pseudocode:

bool completed = false;

void chi
  if completed = false
    create foo;
    create bar;

   completed = true 
  end if

  while looping
     ...
  loop

  completed = false;
  destroy foo
  destroy bar

And within other portions of your program, check the value of 'completed' to see whether or not said objects were created?

EDIT: I just reread your question and edited accordingly. If this is a multi-threaded app you'll also need to make sure that your objects are threadsafe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文