Hello,
Recently I've been hitting on a problem when using WKExtendedRuntimeSession with SwiftUI
I have created a ObservedObject like this:
@ObservedObject var store = TimerStore()
when the view appears I call upon
store.startTimer()
the timer store object looks like this:
When I debug it with my phone attached it offers no problems and it clearly prints out the start and end of the session, even after waiting for 10min (when the session would be expired), however when I keep using the app after debugging the extended runtime session doesn't work anymore on the device.
I've tried a lot but it's blowing my mind. When talking with a Apple developer via Labs last week he said it could be because views in SwiftUI are not permanent .
I do get it to work when I use a storyboard + WatchKit
Does anyone has any idea how I can potentially fix this?
Recently I've been hitting on a problem when using WKExtendedRuntimeSession with SwiftUI
I have created a ObservedObject like this:
@ObservedObject var store = TimerStore()
when the view appears I call upon
store.startTimer()
the timer store object looks like this:
Code Block class TimerStore : NSObject, ObservableObject, WKExtendedRuntimeSessionDelegate { @Published var secondsPassed : Int { didSet { didChange.send() } } var timer : Timer! var extSession: WKExtendedRuntimeSession! init(seconds: Int = 0) { self.secondsPassed = seconds } var didChange = PassthroughSubject<Void, Never>() func startTimer() { timer = Timer.scheduledTimer( timeInterval: 1, target: self, selector: (#selector(self.updateSeconds)), userInfo: nil, repeats: true ) startSession() } @objc func updateSeconds() { self.secondsPassed += 1 } func startSession() { extSession = WKExtendedRuntimeSession() extSession.delegate = self extSession.start() } func stopSession() { extSession.invalidate() timer.invalidate() } func extendedRuntimeSession(_ extendedRuntimeSession: WKExtendedRuntimeSession, didInvalidateWith reason: WKExtendedRuntimeSessionInvalidationReason, error: Error?) { print("Session stopped: \(Date())") } func extendedRuntimeSessionDidStart(_ extendedRuntimeSession: WKExtendedRuntimeSession) { print("Session started: \(Date())") } func extendedRuntimeSessionWillExpire(_ extendedRuntimeSession: WKExtendedRuntimeSession) { print("Session expired: \(Date())") } }
When I debug it with my phone attached it offers no problems and it clearly prints out the start and end of the session, even after waiting for 10min (when the session would be expired), however when I keep using the app after debugging the extended runtime session doesn't work anymore on the device.
I've tried a lot but it's blowing my mind. When talking with a Apple developer via Labs last week he said it could be because views in SwiftUI are not permanent .
I do get it to work when I use a storyboard + WatchKit
Does anyone has any idea how I can potentially fix this?