Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 过帐通知将停止代码的进一步执行_Ios_Swift - Fatal编程技术网

Ios 过帐通知将停止代码的进一步执行

Ios 过帐通知将停止代码的进一步执行,ios,swift,Ios,Swift,我有一个用于观察网络状态的单例对象,它基于NWPathMonitor类 在pathUpdateHandler回调中,我想发布一个具有当前接口类型的路径的自定义通知,然后最奇怪的事情发生了:我在.post方法之前和之后放置了断点,第二个断点从未到达,通知也没有发布。可能是什么问题?这是我第一次遇到这种情况 class NetworkMonitor { static let shared = NetworkMonitor() private let monitor: NW

我有一个用于观察网络状态的单例对象,它基于NWPathMonitor类

pathUpdateHandler
回调中,我想发布一个具有当前接口类型的路径的自定义通知,然后最奇怪的事情发生了:我在.post方法之前和之后放置了断点,第二个断点从未到达,通知也没有发布。可能是什么问题?这是我第一次遇到这种情况

    class NetworkMonitor {

static let shared = NetworkMonitor()

        private let monitor: NWPathMonitor

        enum InterfaceType: String {
            case wifi
            case cellular
            case other
            case none
        }

        private init() {
            monitor = NWPathMonitor()
            monitor.pathUpdateHandler = { path in
                print("Before notification")

                NotificationCenter.default.post(name: NetworkMonitor.connectionChangedNotification, object: nil, userInfo: ["interface": path.interfaceType])

                print("Notification posted") // This line is not being printed
            }
        }

        public func start() {
            let queue = DispatchQueue.global(qos: .utility)
            monitor.start(queue: queue)
        }
    }

    fileprivate extension NWPath {
        var interfaceType: NetworkMonitor.InterfaceType {
            guard status == .satisfied else {
                return .none
            }
            if usesInterfaceType(.wifi) {
                return .wifi
            } else if usesInterfaceType(.cellular) {
                return .cellular
            } else {
                return .other
            }
        }
    }
编辑:

我在两个地方订阅了这个通知,A和B。我注意到对象B中的回调被调用了两次,而不是一次。我不知道这里发生了什么,因为现在我已经注释掉了B中的代码,一切都正常工作,但我的意思是,这到底是怎么回事

在这两个物体中我都有

var connectionObserver: Any?

connectionObserver = NotificationCenter.default.addObserver(forName: 
NetworkMonitor.connectionChangedNotification, object: nil, queue: nil) {
...
}
如果我不使用connectionObserver变量,这个bug也会存在


更改通知名称的
rawValue
不会更改任何内容

谁捕获了通知?我不确定post实现是否正确,但如果post立即通知观察者,则其中一个可能会导致此问题。实际上,这不是一个单音。我已更新了代码。对象B中的块在捕捉到通知时做了什么?谁捕捉到通知?我不确定post实现是否正确,但如果post立即通知观察者,那么其中一个可能会导致此问题。实际上,这不是一个单音。我已经更新了代码。对象B中的块在捕捉到通知时做了什么?