开发者

Is circular method calling allowed?

开发者 https://www.devze.com 2023-04-12 08:07 出处:网络
I am calling a method from two threads and thought I tr开发者_如何学JAVAy this: - (void)hideEnterButton

I am calling a method from two threads and thought I tr开发者_如何学JAVAy this:

- (void)hideEnterButton
{
    if ([NSThread isMainThread])
    {
        enterButton.hidden = YES;
    }
    else
    {
        [self performSelectorOnMainThread:@selector(hideEnterButton) withObject:nil waitUntilDone:NO];
    }
}

Reason for this is that I understand that all GUI handling must be done on the main thread. Will this work?


The proper term is recursive, not circular and yes, it's perfectly fine to do this.


Yes, this is completely valid. I have used this exact method before, and it works very well. The only problem is, any code using this cannot assume that the button is hidden after it returns because it doesn't wait for the call to finish. If that won't be a problem, use it as is. If it will, simply pass YES for waitUntilDone: instead.


Yes. This is a common pattern.

0

精彩评论

暂无评论...
验证码 换一张
取 消