NSOperation and NSNotificationCenter on the main thread

后端 未结 4 1625
挽巷
挽巷 2020-12-12 23:02

I have an NSOperation. When it is finished I fire a NSNotificationCenter to let the program know that the NSoperation is finished and to update the gui.

To my under

4条回答
  •  粉色の甜心
    2020-12-12 23:30

    You can use performSelectorOnMainThread:withObject:waitUntilDone: with using a helper method, in a similar fashion to the following example.

    .....
    [self performSelectorOnMainThread:@selector(fireNotification) withObject:nil waitUntilDone:YES];
    ...
    
    - (void)fireNotification {
      [[NSNotificationCenter defaultCenter] postNotificationName:@"myEventName" object:self]; 
    }
    

    If you don't wait until being done, you will need to consider the cases where other threads may refer to the object which could be already cleaned up before the main thread gets invoked.

提交回复
热议问题