Hi there,
I'm using WCSession to communicate watchOS companion with its iOS app. Every time watch app becomes "active", it needs to fetch data from iOS app, which works e.g. turning my hand back and forth.
But only when the app is opened after it was minimised by pressing digital crown, it didn't fetch data. My assumption is that scenePhase doesn't emit a change on reopen.
Here is the ContentView of watch app:
import SwiftUI
struct ContentView: View {
@EnvironmentObject private var iOSAppConnector: IOSAppConnector
@Environment(\.scenePhase) private var scenePhase
@State private var showOpenCategories = true
var body: some View {
NavigationStack {
VStack {
if iOSAppConnector.items.isEmpty {
WelcomeView()
} else {
ScrollView {
VStack(spacing: 10) {
ForEach(iOSAppConnector.items, id: \.self.name) { item in
ItemView(item: item)
}
}
}
.task {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
loadItems()
}
}
.onChange(of: scenePhase, initial: true) { newPhase, _ in
if newPhase == .active {
loadItems()
}
}
}
fileprivate func loadItems() -> Void {
if iOSAppConnector.items.isEmpty {
iOSAppConnector.loadItems()
}
}
}
What could be the issue? Thanks. Best regards Sanjeev
Can you simplify this sentence, please? It currently doesn't make much sense to me:
But only when the app is opened after it was minimised by pressing digital crown, it didn't fetch data.
It might help for you to log something to the console to show what the scenePhase
is now, and every time the scenePhase
changes (you already have the .onChange
to do that part). Print out the phase, and the count of items in iOSAppConnector.items
.
Then, run through every possible scenario, e.g. while the app is open an on the screen, when it's been opened and is now in the background etc.
You'll be able to see what you did to make the scenePhase
change, and may see what you have to do to correct the issue.