Publishing changes from within view updates is not allowed, this will cause undefined behaviour
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about the SwiftUI 'publishing changes from within view updates' warning, since it shows a working @Published-to-@State workaround.
Recommended by 1 people across 1 different months, 2022 to 2023.
What it is
A SwiftUI tutorial by Rebeloper (Rebel Developer) explaining the purple runtime warning 'Publishing changes from within view updates is not allowed, this will cause undefined behaviour'. It recreates the warning, explains the cause, and fixes it by syncing @Published properties with local @State, then refactors the code.
What people said
Recommends Ahmed Mohiy · 2023-03 · Stack Overflow · on “Deleting an object bound to a TextField that's being edited causes SwiftUI to crash with Fatal error Index out of range”
This happens when at least on of your text flied is in focus mode and the keyboard is visible i think because you are binding to the published array and at the same time you are trying to remove it. So i suggest that you disable delete when any of the textFields are in focus mode. You can do it like this struct Note: Identifiable { var id = UUID() var text = "Hello" } class Category: ObservableObject { @Published var notes = [Note(), Note(), Note()] func deleteAll() { self.notes.removeAll() } } struct TestViewThree: View { @StateObject var category = Category() @State var deleteButtonClicked = false @FocusState var focused: Bool var body: some View { List { ForEach($category.notes) { $notes
Cites markb · 2022-09 · Stack Overflow · on “SwiftUI publishing an environment change from within view update”
Update: 2022-09-28 Xcode 14.1 Beta 3 (finally) fixed the "Publishing changes from within view updates is not allowed, this will cause undefined behavior" See: [LINK] Full disclosure - I'm not entirely sure why this is happening but these have been the two solutions I have found that seem to work. [LINK] [LINK] Example Code // -- main view @main struct MyApp: App { @StateObject private var vm = ViewModel() var body: some Scene { WindowGroup { ViewOne() .environmentObject(vm) } } } // -- initial view struct ViewOne: View { @EnvironmentObject private var vm: ViewModel var body: some View { Button { vm.isPresented.toggle() } label: { Text("Open sheet") } .sheet(isPresented: $vm.isPresented) { Sh
Cites egzoticc · 2022-09 · Stack Overflow · on “SwiftUI publishing an environment change from within view update”
Improved Solution of Rebel Developer as a generic function. Rebeloper solution [this video] It helped me a lot. 1- Create extension for it: extension View{ func sync<T:Equatable>(_ published:Binding<T>, with binding:Binding<T>)-> some View{ self .onChange(of: published.wrappedValue) { published in binding.wrappedValue = published } .onChange(of: binding.wrappedValue) { binding in published.wrappedValue = binding } } } 2- sync() ViewModel @Published var to local @State var struct ContentView: View { @EnvironmentObject var viewModel:ViewModel @State var fullScreenType:FullScreenType? var body: some View { //.. } .sync($viewModel.fullScreenType, with: $fullScreenType)
Cites iKK · 2022-11 · Stack Overflow · on “Using MapKit causes Publishing changes from within view updates is not allowed, this will cause undefined behavior”
Using Swift5.7, XCode14.0, iOS16.0, I get very strange error messages and warnings in my XCode console, when trying to make a MapKit example to work. Here is the log: 2022-11-01 17:26:51.756834+0100 myApp[3999:834036] Metal API Validation Enabled 2022-11-01 17:26:52.139973+0100 myApp[3999:834036] [PipelineLibrary] Mapping the pipeline data cache failed, errno 22 2022-11-01 17:26:52.192482+0100 myApp[3999:834036] [core] "Error returned from daemon: Error Domain=com.apple.accounts Code=7 "(null)"" 2022-11-01 17:26:53.884031+0100 myApp[3999:834036] [SwiftUI] Publishing changes from within view updates is not allowed, this will cause undefined behavior. 2022-11-01 17:26:53.900265+0100 myApp[3999