同步 NSView 调用的好方法是什么?
我有一个从辅助线程接收新数据的视图。每次这样做时,它都应该重新绘制自己。然而,它在运行循环中表现不佳,并且在一段时间后(它是不确定的),我最终在控制台中收到
消息。
我不确定跨线程同步调用 [view setNeedsDisplay:YES]
的正确方法是什么;你能帮助我吗?
为了澄清一点,线程 B(实际上是一个调度队列)通过调用以下命令向视图提供新内容:
-(void)setImageBuffer:(unsigned char*)buffer
{
/* image handling stuff; thread-safe */
[self setNeedsDisplay:YES]; // but this is not thread-safe
}
然后运行运行循环的线程 A 应该重新显示视图。
I have a view that receives new data from a secondary thread. Every time it does, it should redraw itself. However, it doesn't play nice with the run loop, and after some time (it's non-deterministic), I end up getting <Error>: kCGErrorIllegalArgument: CGSUnionRegionWithRect : Invalid region
messages in the console.
I'm not sure what's the right way to synchronize the calls to [view setNeedsDisplay:YES]
across threads; can you help me?
To clarify a little, thread B (actually a dispatch queue) gives new contents to a view by calling this:
-(void)setImageBuffer:(unsigned char*)buffer
{
/* image handling stuff; thread-safe */
[self setNeedsDisplay:YES]; // but this is not thread-safe
}
And then thread A, on which runs the run loop, should redisplay the view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 GCD,你不需要额外的代理方法:
With GCD you don't need the extra proxy method: