簡體   English   中英

有沒有辦法知道觀察者是否收到/沒有收到來自 NSNotificationCenter 的通知?

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

ViewController.m[NSNotificationCenter defaultCenter]注冊為觀察者並且它正在工作。 但是ViewControllerviewDidLoad方法中做了 X 。

我希望 X只有ViewController沒有收到通知時才會發生。

該架構聽起來確實很糟糕,但我的問題是defaultCenter是否有一些數據告訴我們觀察者是否收到通知?

沒有回調方法讓我們知道發送的notification是否被觀察者收到。

根據Notification Center方法,我們可以在 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
}

並且我們可以參考NotificationCenter蘋果文檔,我們應該知道它的功能是:

對象在通知中心注冊以使用 addObserver(_:selector:name:object:) 或 addObserver(forName:object:queue:using:) 方法接收通知(NSNotification 對象)。

並且從文檔中找不到任何提及您的要求。

如果您正在尋找回電,請考慮結合,它的工作方式更像是一種事務性通信協議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM