NSNotification not being sent when postNotificationName: called

后端 未结 7 800
生来不讨喜
生来不讨喜 2020-12-29 08:17

I\'m trying to get one instance of using NSNotificationCenter with addObserver and postNotificationName but I can\'t work out why it w

相关标签:
7条回答
  • 2020-12-29 08:45

    I successfully fixed my "NSNotification not being sent when postNotificationName: called" crash.

    I found the real bug is in notification message handler.

    The postNotificationName and addObserver are all right as the first post of this thread.

    0 讨论(0)
  • 2020-12-29 08:47

    I had a similar issue and my problem was due to the notification being called on another thread. This solved my problem.

    dispatch_async(dispatch_get_main_queue(),^{
        [[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:self];
    });
    
    0 讨论(0)
  • 2020-12-29 08:53

    Have you tried any other names but @"Event" and nil? Just to be sure, you could define your event names in one file and include that into both notification registration and sending. For example:

    Header file:

    extern NSString * const NOTE_myEventName;
    

    Source file:

    NSString * const NOTE_myEventName = @"MyEventName";
    

    Registration:

    [[NSNotificationCenter defaultCenter]
     addObserver:self
        selector:@selector(handleMyEvent:)
            name:NOTE_myEventName
          object:nil];
    

    Notification sending:

    [[NSNotificationCenter defaultCenter]
        postNotificationName:NOTE_myEventName object:nil];
    
    0 讨论(0)
  • 2020-12-29 09:04

    Change this:

    [[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:self];
    

    to this:

    [[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:nil];
    

    If your first notification is registered properly, newEventLoaded should be called.

    0 讨论(0)
  • 2020-12-29 09:05

    I had the same problem. The reason is that I called removeObserver method at

    - (void)viewDidDisappear:(BOOL)animated{
    
        NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    
       [notificationCenter removeObserver:self];
    
    }
    

    So check whether if you had called removeObserver before postNotification.

    Tips: You can search the keyword "removeObserver" to find if you had called this function.

    0 讨论(0)
  • 2020-12-29 09:07

    Basically it's all to do with the order of execution. If you've executed postNotificationName before addObserver, then this is an easy problem to have. Use breakpoints and step through the code :)

    Your first breakpoint should stop here:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView:) name:@"ScanCompleted" object:nil];
    

    Then here:

    [[NSNotificationCenter defaultCenter]postNotificationName:@"ScanCompleted" object:self];
    

    Also, make sure the selector has a colon on. Because it's method signature will be:

    - (void)updateView:(NSNotification *)notification;
    
    0 讨论(0)
提交回复
热议问题