WKExtendedRuntimeSession doesn't work when I use SwiftUI

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:

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?
Answered by hiddevdploeg in 646217022
The new @StateObject solves this 🎉
I'm interested in this too. I assume you have upgraded an existing app from watchOS 6 and have an extension delegate class around somewhere--that you're not using the new SwiftUI App struct.

Code Block
@main
struct FrutaApp: App {

Would making your extended runtime delegate object static, or a property of your extension delegate be acceptable solution for you? That object's lifecycle should be 100% isolated from any SwiftUI view.
I can't use the new @main / App structure because I can't seem to find a way to have relationship pages on watchOS with just SwiftUI
Accepted Answer
The new @StateObject solves this 🎉
WKExtendedRuntimeSession doesn't work when I use SwiftUI
 
 
Q