【问题标题】:Notification not received if posted during init?如果在初始化期间发布通知未收到?
【发布时间】:2017-11-29 18:26:36
【问题描述】:

我创建了一个example project 来演示Notification 处理的同步性质。

通知按预期处理 - 模型初始化期间发布的通知除外。

The Swift Programming Language (Swift 4), Two-Phase Initialization 部分解释了初始化的安全检查。在这里,它说:

安全检查 4
初始化程序不能调用任何实例方法,请阅读 任何实例属性的值,或将 self 称为值 直到初始化的第一阶段完成。

在我的示例中,在调用通知方法之前,初始化的第一阶段完成。而且,如下图,我也试过直接调用通知。视图控制器都不会执行任何操作 - 尽管在初始化后调用 时,视图控制器确实会按预期响应。

这是我应该报告的错误吗?还是我只是在初始化一些简单的事情上脑残?

以下是相关型号代码:

class Model {

    let notificationONEName = Notification.Name("NotificationONE")
    let notificationTWOName = Notification.Name("NotificationTWO")

    init() {
        notifyObserversTWO()
        NotificationCenter.default.post(name: notificationTWOName, object: self)
    }

    func notifyObserversONE() {
        print("START \(Model.self).\(#function)")
        NotificationCenter.default.post(name: notificationONEName, object: self)
        print("END \(Model.self).\(#function)")
    }
}

还有,这是视图控制器中的观察者端代码:

import UIKit

class ViewController: UIViewController {

    let notificationONESelector = #selector(didReceiveNotificationONE)
    let notificationTWOSelector = #selector(didReceiveNotificationTWO)

    let model = Model()

    override func viewDidLoad() {
        super.viewDidLoad()

        subscribeToNotifications()
        model.notifyObserversONE()
        model.notifyObserversTWO()
    }

    func subscribeToNotifications() {
        NotificationCenter.default.addObserver(self,
                                           selector: notificationONESelector,
                                           name: model.notificationONEName,
                                           object: model)

        NotificationCenter.default.addObserver(self,
                                           selector: notificationTWOSelector,
                                           name: model.notificationTWOName,
                                           object: nil)
    }

    @objc func didReceiveNotificationONE(notification: Notification) {
        print("START \(ViewController.self).\(#function)")
        exampleUtilityFunction()
        print("END \(ViewController.self).\(#function)")
    }

    @objc func didReceiveNotificationTWO(notification: Notification) {
        print("START \(ViewController.self).\(#function)")
        exampleUtilityFunction()
        print("END \(ViewController.self).\(#function)")
    }

    func exampleUtilityFunction() {
        print("START \(ViewController.self).\(#function)")
        print("END \(ViewController.self).\(#function)")
    }
}

【问题讨论】:

    标签: ios swift notifications ios11 swift4


    【解决方案1】:

    在调用Model.init 时,没有任何东西在观察它。在拨打Model() 很久之后,您才可以拨打subscribeToNotifications

    如果您想响应此通知,您首先需要在调用init 之前观察object: nil(就像第二个通知一样)。这意味着您无法在属性定义中对其进行初始化。您需要调用subscribeToNotifications,然后创建model

    【讨论】:

    • 而且我也无法从模型中检索通知名称常量。呃。绝对是脑残时刻!谢谢,罗伯。
    猜你喜欢
    • 2019-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多