同步 NSView 调用的好方法是什么?

发布于 2024-09-08 06:50:01 字数 525 浏览 2 评论 0原文

我有一个从辅助线程接收新数据的视图。每次这样做时,它都应该重新绘制自己。然而,它在运行循环中表现不佳,并且在一段时间后(它是不确定的),我最终在控制台中收到 : kCGErrorIllegalArgument: CGSUnionRegionWithRect : Invalid Region 消息。

我不确定跨线程同步调用 [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 技术交流群。

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

发布评论

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

评论(2

极致的悲 2024-09-15 06:50:01
-(void)setImageBuffer:(unsigned char*)buffer
{
    /* image handling stuff; thread-safe */

    [self performSelectorOnMainThread:@selector(induceRedraw)
                           withObject:nil
                                      // Don't just copy this; pick one...
                        waitUntilDone:YES or NO];
}

-(void)induceRedraw
{
    [self setNeedsDisplay:YES]; // but this is not thread-safe
}
-(void)setImageBuffer:(unsigned char*)buffer
{
    /* image handling stuff; thread-safe */

    [self performSelectorOnMainThread:@selector(induceRedraw)
                           withObject:nil
                                      // Don't just copy this; pick one...
                        waitUntilDone:YES or NO];
}

-(void)induceRedraw
{
    [self setNeedsDisplay:YES]; // but this is not thread-safe
}
原谅过去的我 2024-09-15 06:50:01

使用 GCD,你不需要额外的代理方法:

dispatch_queue_t q = dispatch_get_main_queue();
dispatch_async(q, ^(void) {
  [self setNeedsDisplay: YES];
});

With GCD you don't need the extra proxy method:

dispatch_queue_t q = dispatch_get_main_queue();
dispatch_async(q, ^(void) {
  [self setNeedsDisplay: YES];
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文