So when i click the NavigationLink on MyDeviceView
, then it's going to called BLEService.connect()
.
If app has not connected successfully, isDeviceConnected
is false
by default, and the view showing Text("Loading")
When the connection succeeds, BLEService.isDeviceConnected
is changed to true by method BLEService.didChangeDevice
, then the view will display Text("Detail")
So now, here is the issue, When isDeviceConnected
becomes true, the ConfigurationView will display the Test("Detail")
, but after 1s it will automatically go back to MyDeviceView
.
This only happens in iOS15, older versions don't.
struct MyDeviceView: View {
@EnvironmentObject private var onboardService: BLEService
var body: some View {
NavigationLink(destination: ConfigurationView().onAppear{
onboardService.connect(to: row.id)
}) {
BaseRowView(title: row.title)
}
}
}
struct ConfigurationView: View {
@EnvironmentObject private var onboardService: BLEService
var body: some View {
if onboardService.isDeviceConnected {
Test("Detail")
} else {
Test("Loading")
}
}
}
final class BLEService: NSObject, ObservableObject {
@Published var isDeviceConnected = false
func connect(to uuid: UUID) {
guard let selectedDevice = bleDeviceList.first(where: {
$0.id == uuid
}) else { return }
TABleManager.shareInstance().connectDevice(selectedDevice.peripheral)
}
}
extension BLEService: ManagerDelegate {
func didConnectDevice(_ device: CBPeripheral) {
isDeviceConnected = true
}
}