Repository navigation
@FetchAll does not load/display data initially when the parent view has a @Environment(\.scenePhase) property #205
Description
Description
Hey, so I've come across a super strange and specific bug. It might be a SwiftUI bug, but I wanted to report it here to be sure.
I have a fairly simple setup (I've recreated it in an example project that I attached below):
@Table
struct Memory: Hashable, Codable {
let id: UUID
var name: String
var tagId: UUID
}
@Table
struct Tag: Hashable, Codable {
let id: UUID
var name: String
}
@Selection
struct MemoryInfo: Identifiable, Hashable, Codable {
var id: UUID { memory.id }
var memory: Memory
var tag: Tag
init(memory: Memory, tag: Tag) {
self.memory = memory
self.tag = tag
}
}Then, I have a SwiftUI view that loads some data using @FetchAll and its .load(_:) method:
struct TestView: View {
@State private var reloadId: UUID = .init()
@FetchAll private var memories: [MemoryInfo]
var body: some View {
Group {
List {
Button("Reload") {
reloadId = UUID()
}
ForEach(memories) { memoryInfo in
Text(memoryInfo.memory.name)
}
}
}
.task(id: reloadId) {
try! await $memories.load(
Memory
.join(Tag.all) { $0.tagId.eq($1.id) }
.select { MemoryInfo.Columns(memory: $0, tag: $1) }
)
}
}
}(In my actual project, the query has some where clauses based on some filter states, but I removed them for simplicity to demonstrate the issue. That's why I'm not putting the query directly in the @FetchAll() initializer)
This is working totally fine and as expected. The view is shown with the correct list of memories.
However, once you embed TestView into a view that has a @Environment(\.scenePhase) property, the initial list of memories is suddenly empty:
struct ContentView: View {
// If you comment out this line, the @FetchAll above succeeds when the view appears
@Environment(\.scenePhase) private var scenePhase
var body: some View {
TestView()
}
}Triggering a reload by pressing the "Reload" button fixes this and the data is correctly loaded and displayed. But it is never displayed when the view first appears.
This only happens with the scenePhase environment value. I've tried multiple other values and nothing else causes this issue.
This is pretty strange 😅 Do you have any idea what's going on here?
Checklist
- I have determined whether this bug is also reproducible in a vanilla SwiftUI project.
- I have determined whether this bug is also reproducible in a vanilla GRDB project.
- If possible, I've reproduced the issue using the
mainbranch of this package. - This issue hasn't been addressed in an existing GitHub issue or discussion.
Expected behavior
The view should be presented with the loaded data (in this example, there is only one row in the database):
Actual behavior
The view is presented with an empty list:
Reproducing project
https://github.com/SwiftedMind/FetchAllDemo
SharingGRDB version information
1.0.0
Sharing version information
2.7.4
GRDB version information
7.6.1
Destination operating system
iOS 26.0
Xcode version information
Xcode 26.0
Swift Compiler version information
swift-driver version: 1.127.14.1 Apple Swift version 6.2 (swiftlang-6.2.0.19.9 clang-1700.3.19.1)
Target: arm64-apple-macosx26.0
Activity