[英]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.