質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%

Q&A

0回答

589閲覧

navigationBarItems で @ObservedObject の関数を実行すると 次から関数を実行できなくなる

sugidodan

総合スコア26

1グッド

0クリップ

投稿2021/10/02 02:44

編集2021/10/04 07:50

モーションセンサの@ObservedObjectの関数を
navigationBarItemsで実行すると1度だけ実行できますがが、2回目以降は実行できません。

navigationBarItemsの外側では所望動作です。
なぜでしょう?

struct ContentView: View { @ObservedObject var builtIn = MotionSensor() var body: some View { NavigationView { VStack { Button(action: { if builtIn.isStarted { builtIn.stop() }else{ builtIn.start() } }) { if builtIn.isStarted { Text("Disconnect") .foregroundColor(Color.red) .padding() }else{ Text("Connect") .padding() } } ... 中略 HStack{ Text(String(format:"%6.2f", builtIn.roll * 180.0/Double.pi)).padding() Text(String(format:"%6.2f",builtIn.pitch * 180.0/Double.pi)).padding() Text(String(format:"%6.2f",builtIn.azimuth * 180.0/Double.pi)).padding() } ... 中略 ... }.navigationBarItems(leading: Button(action: { if builtIn.isStarted { builtIn.stop() }else{ builtIn.start() } print("tapped!") }) { if builtIn.isStarted { Text("Disconnect") .foregroundColor(Color.red) .padding() }else{ Text("Connect") .padding() } }) } .navigationViewStyle(StackNavigationViewStyle())

@ObservedObjectは以下です。

import Foundation import CoreMotion class MotionSensor: NSObject, ObservableObject { @Published var isStarted = false let motionManager = CMMotionManager() func start() { // start monitoring sensor data if motionManager.isDeviceMotionAvailable { motionManager.deviceMotionUpdateInterval = 0.1 motionManager.startDeviceMotionUpdates(to: OperationQueue.current!, withHandler: {(motion:CMDeviceMotion?, error:Error?) in self.updateMotionData(deviceMotion: motion!) }) } isStarted = true } func stop() { isStarted = false motionManager.stopDeviceMotionUpdates() } @Published var pitch = 0.0 @Published var roll = 0.0 @Published var azimuth = 0.0 private func updateMotionData(deviceMotion:CMDeviceMotion) { pitch = (deviceMotion.attitude.pitch) roll = (deviceMotion.attitude.roll) azimuth = (deviceMotion.attitude.yaw) }
退会済みユーザー👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2021/10/02 03:53

実機 iPhone 8 で試したのですが、バーアイテム・ボタンのどちらも問題なく `Connect`/`Disconnect` の切り替えを何度でも行えますよ。こちらで実装した `updateMotionData(deviceMotion:)` は `print("x: \(deviceMotion.gravity.x)")` するだけの単純なものですが、データの取得がちゃんと切り替わっているのは確認できました。ご提示のコード外の問題の可能性が大きいように感じます。
sugidodan

2021/10/04 07:49

ありがとうございます。 失礼しました。追記します。 @ObservedObjectのupdateMotionData内をコメントアウトすると `Connect`/`Disconnect` の切り替えを何度もできますが、 Textが更新されなくなります。 以下を追記 ---- var body: some View { NavigationView {     VStack {     .... HStack{ Text(String(format:"%6.2f", builtIn.roll * 180.0/Double.pi)).padding() Text(String(format:"%6.2f",builtIn.pitch * 180.0/Double.pi)).padding() Text(String(format:"%6.2f",builtIn.azimuth * 180.0/Double.pi)).padding() } ---- @Published var pitch = 0.0 @Published var roll = 0.0 @Published var azimuth = 0.0 private func updateMotionData(deviceMotion:CMDeviceMotion) { pitch = (deviceMotion.attitude.pitch) roll = (deviceMotion.attitude.roll) azimuth = (deviceMotion.attitude.yaw) }
退会済みユーザー

退会済みユーザー

2021/10/04 10:45

なるほど、再現できました。「1度だけ実行できる」ではなく値の取得中はナビゲーションバーアイテムのほうだけ反応が鈍いだけのようです。かなりシビアですが、タイミングが合うとバーのほうでも Connect 解除できます。 - `motionManager.deviceMotionUpdateInterval` を `0.2` や `0.5` に和らげると反応するタイミングが改善 - ビューへの値の反映の有無は関係ない(値を表示する `Text` があってもなくても問題が発生する) - `@Published` なプロパティの値を高頻度で更新していると発生。プロパティが一つでも発生(個数の問題ではない) うーん、根本的な解決策が分からないですね…………!テクニカルインシデントの残数があればAppleに訊きたいくらいです。
sugidodan

2021/10/05 07:19

ありがとうございます。 buttonの場合は高頻度であっても所望動作です。 motionManager.deviceMotionUpdateInterval = 0.1 なので、以下に関連していると考えています。 あってそうでしょうか? https://inon29.hateblo.jp/entry/2020/04/05/161913
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問