I've encountered an interesting issue while working on a SwiftUI project, specifically when using @Environment(\.dismiss) along with a callback function like onSuccess: (Bool) -> Void in a ListDetail view. The app starts freezing, and CPU utilization spikes to 99%.
The repository now contains two branches:
issue/swiftui-freezing: This branch keeps the original code that reproduces the issue as encountered.main: This branch contains the fixed code, which resolves the freezing problem.
-
Scenario:
- Using
@Environment(\.dismiss)in theListDetailview. - Including a callback function,
onSuccess: (Bool) -> Void, to handle success scenarios for ListDetail.
- Using
-
Symptoms:
- The app freezes when these two components are used together.
- CPU utilization spikes to 99%, causing performance degradation.
-
Workaround:
- If I comment out
@Environment(\.dismiss), in theListDetailview. or - Remove the
onSuccesscallback from ListDetail View, the app works perfectly fine without freezing.
- If I comment out
-
Open the project.
-
Check these two files that are causing the issue:
- ListDetail.swift
- ListView.swift
In both files, the combination of
@Environment(\.dismiss)and theonSuccesscallback is causing the freeze.
This issue raises the question of whether there is a potential conflict when using @Environment(\.dismiss) alongside a callback in SwiftUI.
- Xcode: 15.2
- iOS: 17.2
- macOS: 14.6.1
