wp42
wp42

Reputation: 178

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?

Upvotes: 0

Views: 831

Answers (2)

polymerchm
polymerchm

Reputation: 228

If you are looking for a call back, consider Combine, which works more like a transactional communications protocol.

Upvotes: 0

aircraft
aircraft

Reputation: 26924

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.

Upvotes: 1

Related Questions