[英]Why is my posted notification not being received in my Swift iOS app
我的应用程序中有三个自定义 swift 类。 一个是NetworkServices
class,另外两个是自定义的UIViewController
类。 NetworkServices class 在打开 wifi 且有网络连接时以及在关闭 wifi 且没有网络连接时向其他两个类发送通知。 目前,应用程序开始发布isConnected
通知并且代码按预期执行。 然后我关闭 wifi 并发布isDisconnected
并且代码按预期执行。 但是,当我再次打开 wifi 时,已发送isConnected
帖子,但两个自定义UIViewController
类中的任何一个都没有收到它。 我的问题是为什么不呢?
这是我的NetworkServices
class 中的相关代码行;
protocol NetworkServicesDelegate: AnyObject {
func sendStockInfo(stocksInfo: [String: StockInfo])
}
final class NetworkServices {
static let sharedInstance = NetworkServices()
...
public func startMonitoring() {
monitor.start(queue: queue)
self.monitor.pathUpdateHandler = { [weak self] path in
if path.status == .satisfied {
// connect the socket
self?.socket.connect()
print("DEBUG: self?.socket.connect() called")
} else {
// disconnect the socket
self?.socket.disconnect()
// print("DEBUG: self?.socket.disconnect() called")
self?.isConnected = false
// post notification that socket is now disconnected
DispatchQueue.main.async {
print("DEBUG: Notification.Name(rawValue) called")
let name = Notification.Name(rawValue: isDisconnectedNotificationKey)
NotificationCenter.default.post(name: name, object: nil)
}
}
}
}
...
}
extension NetworkServices: WebSocketDelegate {
func didReceive(event: WebSocketEvent, client: WebSocket) {
switch event {
case .connected(_):
self.isConnected = true
// post notification that socket is now connected
let name = Notification.Name(rawValue: isConnectedNotificationKey)
NotificationCenter.default.post(name: name, object: nil)
print("DEBUG: Notification isConnected posted")
case .disconnected(let reason, let code):
print("DEBUG: Got disconnected reason = \(reason) code = \(code)")
self.isConnected = false
case .cancelled:
print("DEBUG: cancelled.")
// reconnect socket
socket.connect()
self.isConnected = true
case .reconnectSuggested(let suggestReconnect):
print("DEBUG: suggestReconnect = \(suggestReconnect)")
// post notification that socket is not connected
case .viabilityChanged(let viabilityChanged):
print("DEBUG: viabilityChanged = \(viabilityChanged)")
case .error(let error):
print("DEBUG error: \(String(describing: error?.localizedDescription))")
case .text(let socketString):
// print("DEBUG: .text available")
parseJSONSocketData(socketString)
default:
break
}
}
}
这是我的两个自定义UIViewController
类中的相关代码行;
protocol CompanyPriceListDelegate: AnyObject {
func sendPriceHistory(_ priceHistory: PriceHistory)
}
class CompanyPriceListVC: UITableViewController {
...
let isConnected = Notification.Name(rawValue: isConnectedNotificationKey)
let isNotConnected = Notification.Name(rawValue: isDisconnectedNotificationKey)
override func viewDidLoad() {
super.viewDidLoad()
...
createObservers()
...
}
deinit {
NotificationCenter.default.removeObserver(self)
}
private func createObservers() {
print("DEBUG: createObservers() called")
NotificationCenter.default.addObserver(self, selector: #selector(CompanyPriceListVC.dismissAndFetchStockInfo), name: isConnected, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(CompanyPriceListVC.notifyUserOnScreen(notification:)), name: isNotConnected, object: nil)
}
@objc private func fetchStockInfo() {
NetworkServices.sharedInstance.fetchStockInfo(symbols: companyStockSymbols, delegate: self)
}
@objc private func notifyUserOnScreen(notification: NSNotification) {
self.present(alertController, animated: true)
}
@objc public func dismissAndFetchStockInfo() {
if presentedViewController == alertController {
self.alertController.dismiss(animated: true, completion: nil)
}
fetchStockInfo()
}
...
}
//
// CompanyDetailsVC.swift
// BetVictorTask
//
// Created by Stephen Learmonth on 02/03/2022.
//
import UIKit
class CompanyDetailsVC: UIViewController {
...
let isConnected = Notification.Name(rawValue: isConnectedNotificationKey)
let isNotConnected = Notification.Name(rawValue: isDisconnectedNotificationKey)
override func viewDidLoad() {
super.viewDidLoad()
configureNavigationBar()
addSubViews()
activateConstraints()
createObservers()
fetchCompanyDetails(symbol: symbol)
let controller = navigationController?.viewControllers.first as! CompanyPriceListVC
controller.delegate = self
}
deinit {
NotificationCenter.default.removeObserver(self)
}
private func createObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(CompanyDetailsVC.dismissAndFetchCompanyDetails), name: isConnected, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(CompanyDetailsVC.notifyUserOnScreen(notification:)), name: isNotConnected, object: nil)
}
@objc private func dismissAndFetchCompanyDetails() {
print("DEBUG: dismissAndFetchCompanyDetails() called")
if presentedViewController == alertController {
self.alertController.dismiss(animated: true, completion: nil)
}
fetchCompanyDetails(symbol: symbol)
}
@objc private func notifyUserOnScreen(notification: NSNotification) {
print("DEBUG: notifyUserOnScreen(notification) called")
self.present(alertController, animated: true)
}
...
}
extension CompanyDetailsVC: CompanyPriceListDelegate {
func sendPriceHistory(_ priceHistory: PriceHistory) {
self.updatePrices(priceHistory)
}
}
我正在真实设备上进行测试。
从 .cancelled 案例中删除了 socket.connect()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.