gpt4 book ai didi

ios - Swift 通知中心导致内存泄漏

转载 作者:行者123 更新时间:2023-11-29 05:32:45 30 4
gpt4 key购买 nike

第一次海报。我对 Swift、编码和一般知识都很陌生,并且遇到了一个我似乎无法解决的问题。

在我的代码中,我有两个 View Controller 。第一个 View Controller 允许用户查看蓝牙设备,并选择要连接的设备。当用户选择一个设备时,它会转到第二个 View Controller ,该 Controller 显示来自蓝牙设备的温度数据。

这一切都工作得很好,但如果我回到第一个 View Controller ,然后再次选择相同的设备,我现在会从该设备收到两个相同的温度读数。 (蓝牙设备正在从我的代码接收两个相同的命令,并发送回两个值)。

基本上,每次我在 View Controller 之间来回切换时,似乎都会创建 View Controller 的另一个实例,从而造成内存泄漏。 (如果我来回连续五次,每次单击按钮接收一个值,我都会收到五个蓝牙读数)

我相信我的问题在于我创建和解雇通知中心观察者,但我似乎无法找出正确的解决方案。

我遗漏了我认为与我的问题无关的代码,因此如果我缺少解决问题所需的任何代码,请告诉我。任何帮助将不胜感激!

//第一个 View Controller

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("*****************************")
print("Connection complete")
print("Peripheral info: \(String(describing: blePeripheral))")

//Stop Scan- We don't need to scan once we've connected to a peripheral. We got what we came for.
centralManager?.stopScan()
print("Scan Stopped")

//Erase data that we might have
data.length = 0

//Discovery callback
peripheral.delegate = self
//Only look for services that matches transmit uuid
peripheral.discoverServices(nil)

performSegue(withIdentifier: "Go", sender: nil)
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destination = segue.destination as! TempPage
destination.peripheral = blePeripheral
}



func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {

if characteristic == rxCharacteristic {
if let ASCIIstring = NSString(data: characteristic.value!, encoding: String.Encoding.utf8.rawValue) {
characteristicASCIIValue = ASCIIstring
NotificationCenter.default.post(name:NSNotification.Name(rawValue: "Notify"), object: nil)
connectionStatus = "Connected!"
}
}

//第二个 View Controller

override func viewDidLoad() {
super.viewDidLoad()

//Create and start the peripheral manager
peripheralManager = CBPeripheralManager(delegate: self, queue: nil)

//-Notification for updating the text view with incoming text
updateIncomingData()

}


override func viewDidDisappear(_ animated: Bool) {
peripheralManager?.stopAdvertising()
self.peripheralManager = nil
super.viewDidDisappear(animated)
NotificationCenter.default.removeObserver(self)

}

func updateIncomingData () {
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "Notify"), object: nil , queue: nil){
notification in

if characteristicASCIIValue != nil
{
self.rawValue = characteristicASCIIValue as String
print(characteristicASCIIValue)

}
self.batteryLevelLabel.text = ("\(String(batteryLevel))%")

}

@IBAction func returnToFirstViewController(_ sender: Any) {
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
}



}

最佳答案

尝试在通知中心回调中将 self 捕获为无主或弱:

    func updateIncomingData () {
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "Notify"), object: nil , queue: nil) { [unowned self] notification in

if characteristicASCIIValue != nil
{
self.rawValue = characteristicASCIIValue as String
print(characteristicASCIIValue)

}
self.batteryLevelLabel.text = ("\(String(batteryLevel))%")

}

这篇文章可能有用:https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html

关于ios - Swift 通知中心导致内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57379804/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com