iosobjective-cnsnotificationcenternsnotification

Is there a way to know if an observer did / did not get a notification from NSNotificationCenter?


ViewController.m is registered as an observer in [NSNotificationCenter defaultCenter] and it's working. But ViewController does X inside viewDidLoad method.

I would like X to happen only if ViewController did not get the notification.

The architecture might sound broken indeed, but my question is if there is some data inside defaultCenter the tells us if an observer got a notification?


Solution

  • There is no call-back methods for us to know if the sended notification if is recieved by the observer.

    According to the Notification Center methods we can get in Xcode:

    /****************   Notification Center ****************/
    open class NotificationCenter : NSObject {
    
    
        open class var `default`: NotificationCenter { get }
    
    
        open func addObserver(_ observer: Any, selector aSelector: Selector, name aName: NSNotification.Name?, object anObject: Any?)
    
    
        open func post(_ notification: Notification)
    
        open func post(name aName: NSNotification.Name, object anObject: Any?)
    
        open func post(name aName: NSNotification.Name, object anObject: Any?, userInfo aUserInfo: [AnyHashable : Any]? = nil)
    
    
        open func removeObserver(_ observer: Any)
    
        open func removeObserver(_ observer: Any, name aName: NSNotification.Name?, object anObject: Any?)
    
    
        @available(iOS 4.0, *)
        open func addObserver(forName name: NSNotification.Name?, object obj: Any?, queue: OperationQueue?, using block: @escaping (Notification) -> Swift.Void) -> NSObjectProtocol
    }
    

    And we can refer to NotificationCenter's apple docs , we should learn its function is:

    Objects register with a notification center to receive notifications (NSNotification objects) using the addObserver(_:selector:name:object:) or addObserver(forName:object:queue:using:) methods.

    And from docs can not find any mention of your requirement.