Multithreaded NSView:setNeedsDisplay:

  • Hello,

    I'm trying to find the "correct" way to request a view to repaint from
    a thread other than the main one. I've seen this question asked
    before, but I haven't seen an answer that is both (1) elegant, and (2)
    works. I was hoping that someone could point me in the right direction.

    Say I have this:
    NSView* view;
    and I want to call
    [view setNeedsDisplay:YES];
    from a thread other than main.

    From what I've read elsewhere, I would love to do something like this:

    [view performSelectorOnMainThread:@selector(setNeedsDisplay:)
    withObject:YES waitUntilDone:NO];

    Unfortunately, this doesn't work because YES is not an object. My
    current solution is to create a proxy method:

    - (void) redisplay
    {
           [self setNeedsDisplay:YES];
    }

    And call it like this:

    [view performSelectorOnMainThread:@selector(redisplay) withObject:YES
    waitUntilDone:NO];

    But this is ugly. Is there a better way? Bummer that setNeedsDisplay:
    is not threadsafe. Bummer that it requires an argument at all...

    Cheers,
    -Rony Kubat
  • > [view performSelectorOnMainThread:@selector(redisplay)
    > withObject:YES waitUntilDone:NO];
    >
    > But this is ugly. Is there a better way? Bummer that
    > setNeedsDisplay: is not threadsafe. Bummer that it requires an
    > argument at all...

    I had answered this off-list, but just to reiterate

    this is probably the best way to do this

    [view performSelectorOnMainThread:@selector(redisplay) withObject:nil
    waitUntilDone:NO];

    and have the redisplay stub method as part of your view subclass.
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today