NSOperation blocks UI painting?

后端 未结 2 410
长发绾君心
长发绾君心 2021-02-04 09:39

I\'m after some advice on the use of NSOperation and drawing:

I have a main thread create my NSOperation subclass, which then adds it to an

2条回答
  •  孤城傲影
    2021-02-04 10:24

    Most people know that any display-related task must be done on the main thread. However, as a direct consequence of that, any change to a Cocoa bindings property that may affect the drawing must also be modified on the main thread (because the KVO triggers will be handled on the thread from which they are triggered). This is a surprise to most people: in particular, it is not safe to call [self setNeedsDisplay] from a thread other than the main thread.

    As mentioned by gerry, your NSNotification is not processed on the main thread, it is processed on the thread from which it is sent. Hence in your NSNotification handler, you must send your commands back to the main thread if they are display-related or if they affect Cocoa bindings. @performSelector works, but with queues, I have found an easier and more readable method:

    [ [ NSOperationQueue mainQueue] addOperationWithBlock:^(void) {
        /* Your main-thread code here */ }];
    

    It avoids the definition of a secondary helper function whose only purpose is to be called from the main thread. mainQueue was defined only in >10.6.

提交回复
热议问题