Compiler gives warning on performSelectorOnMainThread:@selector(delegateMethod)

后端 未结 4 2095
渐次进展
渐次进展 2021-01-05 02:29

I have an NSOperation that wraps some web service functionality. The NSOperation has a delegate that is to be messaged when the operation is over.

As the NSOperation

4条回答
  •  自闭症患者
    2021-01-05 02:53

    It might be even a better solution not call performSelectorOnMainThread: on a delegate or other protocol implementation. Make it the responsibility of the delegate/receiver to determine if it needs to do things on the main thread.

    [delegate performSelector:@selector(delegateAction:)
                   withObject:actionData];
    

    Delegate implementation

    - (void)delegateAction:(NSData*)actionData
    {
        [self performSelectorOnMainThread:@selector(updateUIForAction:)
                               withObject:actionData
                            waitUntilDone:NO];
    }
    
    - (void)updateUIForAction:(NSData*)actionData
    {
        // Update your UI objects here
    }
    

    It might look like more code, but the responsibility is in the right place now

提交回复
热议问题