簡體   English   中英

將調用同步到NSView的好方法是什么?

[英]What's the good way to synchronize calls to a NSView?

我有一個從輔助線程接收新數據的視圖。 每次這樣做時,它應該重新繪制自己。 但是,它在運行循環中不能很好地運行,並且經過一段時間(不確定性),我最終得到了<Error>: kCGErrorIllegalArgument: CGSUnionRegionWithRect : Invalid region控制台中的<Error>: 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應該重新顯示該視圖。

-(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
}

使用GCD,您不需要額外的代理方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM