比方说,在我的 GUI 应用程序中(它可以跨 OSX/iOS 移植),我有多个线程可以向主应用程序线程发送通知。通知应该在处理 Cocoa 的输入事件和重绘请求的同一上下文中处理。我的目标是在屏幕上反射(reflect)来自并发线程的某些事件。
在 Windows 上有 PostMessage(),那么它在 OS X 和 iOS 上的模拟是什么?
dispatch_async()
似乎是一个接近的候选者,但是,文档说:
The target queue determines whether the block is invoked serially or concurrently with respect to other blocks submitted to that same queue.
说实话,这除了造成困惑之外什么也没说。另外,尚不清楚哪个消息队列与dispatch_async一起使用,以便根据GUI事件串行处理消息。
最佳答案
你是对的,dispatch_async 是正确的选择。您应该寻找的队列是主队列:
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Got to main thread.");
});
关于ios - 将消息发布到 Cocoa 应用程序的主事件循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17265657/