简体   繁体   English

观察者从未打电话

[英]Observer never called

I have two functions 我有两个功能

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        NSNotificationCenter.defaultCenter().addObserverForName("personalDataDidLoad", object: self, queue: NSOperationQueue.mainQueue()) {_ in
            print("Received notification")
            self.showPersonalData()
        }

        loadPersonalData()
    }



func loadPersonalData() {
    //load data
    print("personal data loaded")
    NSNotificationCenter.defaultCenter().postNotificationName("personalDataDidLoad", object: nil)

but for some reason this outputs 但是由于某种原因

 personal data loaded

instead of the expected 而不是预期的

 personal data loaded
 Received notification

I'm probably missing something obvious, but I don't see it right now.... 我可能缺少明显的东西,但现在看不到...。

I also tried addObserver with selector: "showPersonalData:" but this throws an unrecognized selector exception.. 我还尝试addObserver带有selector: "showPersonalData:"但这会引发无法识别的选择器异常。

The problem is with the 2nd parameter in postNotificationName and addObserverForName : object . 问题出在postNotificationNameaddObserverForNameobject的第二个参数上。 When you add an observer, and pass a non-nil object value, this means that the observer block will run when a notification comes from that object, and from that object only . 当添加观察者并传递非object值时,这意味着当通知来自该对象且仅来自该对象时,观察者块将运行。 However, when you fire the notification, you do object: nil . 但是,当您触发通知时,您将object: nil So your notification is ignored. 因此,您的通知将被忽略。

On the other hand, passing a nil value for object means, "I want to receive this notification regardless of who sends it". 另一方面,传递objectnil值表示“无论谁发送,我都希望接收此通知”。

So you need to make sure the object value is the same in both places: either self or nil . 因此,您需要确保两个地方的对象值都相同: selfnil

Is there a reason you need to use addObserverForName(_:object:queue:usingBlock:) ? 您有理由需要使用addObserverForName(_:object:queue:usingBlock:)吗?

Try this instead: 尝试以下方法:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    NSNotificationCenter.defaultCenter().addObserver(self, "personalDataDidLoadNotification:", name: "personalDataDidLoad" object: nil)
    loadPersonalData()
}

func loadPersonalData() {
    //load data
    print("personal data loaded")
    NSNotificationCenter.defaultCenter().postNotificationName("personalDataDidLoad", object: nil)
}

func personalDataDidLoadNotification(notification: NSNotification) {
    print("notification recieved")
}

Another answer to the title question (but not this example) but will hopefully help others in the situation I have been in for the last 3 hours: 标题问题的另一个答案(但不是此示例),但希望能在我最近3个小时遇到的情况下帮助其他人:

Make sure your notificationcenter observer is added inside a class that has a persisted instance. 确保将您的notificationcenter观察器添加到具有持久实例的类中。 I created the observer inside a class that was called as eg MyClass().setupNotification inside a local method of another class. 我在另一个类的本地方法内的名为MyClass()。setupNotification的类内创建了观察器。

This meant the observer was immediately deleted and didnt persist against any instance. 这意味着观察者被立即删除,并且没有针对任何实例进行持久化。

Schoolboy error - but hope this helps others searching for this. 小学生错误-希望这可以帮助其他人进行搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM