Is there a way to create a new session of WKExtendedRuntimeSession in SwiftUI?

Yotzin Castrejon

I've created a simple countdown timer in watchOS. When you press start the timer starts and session.start is declared as well. Everything works fine in the background. Then when it stops either by the button or by the timer the session will invalidate.

By the way that I'm reading the documentation here

It states, "you can only schedule one session at a time; if you need to reschedule a session, invalidate the current session, and schedule a new one."

So my question is, how do I create a new session in SwiftUI? Here is my code:

import SwiftUI

struct TestView: View {
    @State var start = false
    @State var number = 10
    @State var time = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
    var session = WKExtendedRuntimeSession()
    var body: some View {
        VStack {
            Text("\(number)")
            Spacer()
            Button(action: {
                self.start.toggle()
                if self.start {
                    session.start()
                } else {
                    session.invalidate()
                }
            }) {
                Text(self.start ? "Stop" : "Start")
            }
        }.onReceive(self.time) { (_) in
            if self.start {
                if number > 0 {
                    number -= 1
                } else {
                    self.start.toggle()
                    number = 10
                    session.invalidate()
                }
      }
        
    }
    
}
}
Asperi

It is better to do in class, like Coordinator

class SessionCoordinator {
    private var session: WKExtendedRuntimeSession?

    func start() {
        guard session?.state != .running else { return }

        // create or recreate session if needed
        if nil == session || session?.state == .invalid {
            session = WKExtendedRuntimeSession()
        }
        session?.start()
    }

    func invalidate() {
        session?.invalidate()
    }
}

struct TestView: View {
    let coordinator = SessionCoordinator()
    @State var start = false
    @State var number = 10
    @State var time = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
    var body: some View {
        VStack {
            Text("\(number)")
            Spacer()
            Button(action: {
                self.start.toggle()
                if self.start {
                    coordinator.start()
                } else {
                    coordinator.invalidate()
                }
            }) {
                Text(self.start ? "Stop" : "Start")
            }
        }.onReceive(self.time) { (_) in
            if self.start {
                if number > 0 {
                    number -= 1
                } else {
                    self.start.toggle()
                    number = 10
                    coordinator.invalidate()
                }
            }

        }

    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Has WKExtendedRuntimeSession changed the way timers work?

httpservletrequest - create new session / change session Id

Create new webdriver session manually

how to create new Session in laravel?

Unable to create new remote session

create new session after login

Add new object on Session create

Create new project in new session in R

Is there a way to modify or create a new builtin?

How to create new tmux session if none exists

Create new session/window name that contain dot

Unable to create new Chrome remote session

Create a new session as a copy of another on Livy

SwiftUI - Is there a way to create a read-only TextEditor?

Whats is the best way to create a model of a user in swiftUI

Is there any way to create BottomBar using SwiftUI

Create new tmux session inside a session with default path and name

Create new tmux session from inside a tmux session

How to create a new watson assistant session on exisiting session expiration in Flask?

If named tmux session exists, make new session and group with it, else create that named tmux session

Create a new session with same group-name, preserving current window in new session

What is the proper way to create a new generic struct?

Any way to create a new, empty Binding in Ruby?

Is there a way to create a new directory, containing subdirectories and documents?

Better way to create new pandas comparison dataframe

What is most efficient way to create new activity

Pythonic way to filter columns and then create a new column

Proper way to create an new parcelable object

What is the way to create a new hash from params?