Post

Replies

Boosts

Views

Activity

Reply to Crash After Presenting SwiftUI Alert
Ok, I finally saw a runtime warning in debug when presenting the alert, which is something to go on. But why would this warning be issued? My view model is marked @MainActor. Publishing changes from within view updates is not allowed, this will cause undefined behavior.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’24
Reply to NavigationBarHidden & swipe back
You may be getting the navigation controller’s gesture handling into some state that it’s not intended to handle. I had a similar (or perhaps the same) issue where when swiping back on the root screen (which doesn’t do anything obviously, but nevertheless is possible and as it turns out I do often while trying to scroll vertically), the controller will mess up the transition of the next navigation operation, appearing to 'freeze' until moved to the background and brought back. I solved this by implementing the gesture begin delegate method, and rejecting the gesture when on the root screen. Maybe worth trying for your case? You can see an example of doing this here: https://github.com/siteline/swiftui-introspect/blob/main/Tests/UITestsHostApp/StatusBarStyle/NavigationView.swift#L42. func gestureRecognizerShouldBegin(_: UIGestureRecognizer) -> Bool { viewControllers.count > 1 }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’24
Reply to TaskGroup Crash
Here’s the only code in my codebase that uses TaskGroup. // Function that processes a bunch of 'action triggers' and produces a stream of actions func run(action: Action, find: @escaping (Model.Type) -> Model?) -> AsyncStream<Action> { AsyncStream { continuation in Task { await withTaskGroup(of: Void.self) { group in for trigger in triggers { group.addTask { for await result in trigger(action, find) { if result is VoidAction { continue } continuation.yield(result) } } } await group.waitForAll() continuation.finish() } } } } // Later, from an async context. Process an action and dispatch its output. Task { for await output in run(action: action, find: { store.find($0) }) { try await store.dispatch(output) } } I was able to solve the crash by explicitly marking both the task group’s closures as @MainActor. But would love to get a deeper understanding of why this doesn’t work. await withTaskGroup(of: Void.self) { @MainActor group in for trigger in triggers { group.addTask { @MainActor in for await result in trigger(action, find) { if result is VoidAction { continue } continuation.yield(result) } } } await group.waitForAll() continuation.finish() }
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’22
Reply to Crash After Presenting SwiftUI Alert
Ok, I finally saw a runtime warning in debug when presenting the alert, which is something to go on. But why would this warning be issued? My view model is marked @MainActor. Publishing changes from within view updates is not allowed, this will cause undefined behavior.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jul ’24
Reply to NavigationBarHidden & swipe back
You may be getting the navigation controller’s gesture handling into some state that it’s not intended to handle. I had a similar (or perhaps the same) issue where when swiping back on the root screen (which doesn’t do anything obviously, but nevertheless is possible and as it turns out I do often while trying to scroll vertically), the controller will mess up the transition of the next navigation operation, appearing to 'freeze' until moved to the background and brought back. I solved this by implementing the gesture begin delegate method, and rejecting the gesture when on the root screen. Maybe worth trying for your case? You can see an example of doing this here: https://github.com/siteline/swiftui-introspect/blob/main/Tests/UITestsHostApp/StatusBarStyle/NavigationView.swift#L42. func gestureRecognizerShouldBegin(_: UIGestureRecognizer) -> Bool { viewControllers.count > 1 }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Feb ’24
Reply to TaskGroup Crash
Here’s the only code in my codebase that uses TaskGroup. // Function that processes a bunch of 'action triggers' and produces a stream of actions func run(action: Action, find: @escaping (Model.Type) -> Model?) -> AsyncStream<Action> { AsyncStream { continuation in Task { await withTaskGroup(of: Void.self) { group in for trigger in triggers { group.addTask { for await result in trigger(action, find) { if result is VoidAction { continue } continuation.yield(result) } } } await group.waitForAll() continuation.finish() } } } } // Later, from an async context. Process an action and dispatch its output. Task { for await output in run(action: action, find: { store.find($0) }) { try await store.dispatch(output) } } I was able to solve the crash by explicitly marking both the task group’s closures as @MainActor. But would love to get a deeper understanding of why this doesn’t work. await withTaskGroup(of: Void.self) { @MainActor group in for trigger in triggers { group.addTask { @MainActor in for await result in trigger(action, find) { if result is VoidAction { continue } continuation.yield(result) } } } await group.waitForAll() continuation.finish() }
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Apr ’22