block中多重引用
2020-10-10 本文已影响0人
zaq1125
device.connect({ [weak self] (tmpDevice, success) in
guard let self = self else { return }
if success {
// 连接成功
device.discoverCharacteristics(for: self.Service_UUID) { (success) -> (Void) in
if success {
device.subscribe(forService: self.Service_UUID, characteristic: self.NOTIFY_Characteristic_UUID) { (device, serviceData, characterData, success) in
if let completion = completion {
completion(device, true)
}
}
device.readCharacteristicCallback = ({ [weak self] (characteristic, model) in
if let model = model {
self?.receiveNotifyModel(model: model)
}
})
}
else {
self.connectToDevice(device: device, curCount: curCount - 1, completion)
}
}
}
else {
self.connectToDevice(device: device, curCount: curCount - 1,completion)
}
})
在第一重block中如果使用了
guard let self = self else { return }
那么接下来的内部的每一重block,用到的self都是强指针,都要使用[weak self]防止强引用。
如果第一重block中,只使用了[weak self],不使用
guard let self = self else { return }
那么后面的block中也没必要使用[weak self],否则会出错。