Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call setNeedsDisplayInRect using performSelectorOnMainThread?

How to call setNeedsDisplayInRect using performSelectorOnMainThread? The problem is rect. I do not know how to pass the rect in performSelectorOnMainThread method. This method ask NSObject, but CGRect is not NSObject, it is just structure *.

//[self setNeedsDisplayInRect:rect];
[self performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:0 waitUntilDone:YES];
}

-(void)drawRect:(CGRect)rect {

    /// drawing...

}

I need to invoke setNeedsDisplayInRect method in MainThread from not Main Thread. Does anyone know how to do it?????????? Thanks in advance..

Really Thanks.

like image 571
mooongcle Avatar asked Sep 16 '26 11:09

mooongcle


1 Answers

If you're on iOS 4.0 or later, you can use the following

dispatch_async(dispatch_get_main_queue(), ^{
    [self setNeedsDisplayInRect:theRect];
});

On iOS 3.2 and earlier, you can set up an NSInvocation and run that on the main thread:

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(setNeedsDisplayInRect:)]];
[invocation setTarget:self];
[invocation setSelector:@selector(setNeedsDisplayInRect:)];
// assuming theRect is my rect
[invocation setArgument:&theRect atIndex:2];
[invocation retainArguments]; // retains the target while it's waiting on the main thread
[invocation performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:YES];

You may want to set waitUntilDone to NO unless you absolutely need to wait for this call to finish before proceeding.

like image 60
Lily Ballard Avatar answered Sep 18 '26 03:09

Lily Ballard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!