🐞 Runtime Error: Publishing changes from within view updates
Description
When running the app and opening a file in a project, hundreds of runtime errors (purple) are generated on
var selectionStateinWorkspaceDocument.swift::31- https://github.com/CodeEditApp/CodeEdit/blob/a0848b9f6385a8bb0fab05e01f0daf3b4a3c4d25/CodeEdit/Documents/WorkspaceDocument.swift#L31
var preferencesinAppPreferencesModel.swift::35- https://github.com/CodeEditApp/CodeEdit/blob/a0848b9f6385a8bb0fab05e01f0daf3b4a3c4d25/CodeEditModules/Modules/AppPreferences/src/Model/AppPreferencesModel.swift#L35
var selectedLightThemeinThemeModel.swift::39- https://github.com/CodeEditApp/CodeEdit/blob/a0848b9f6385a8bb0fab05e01f0daf3b4a3c4d25/CodeEditModules/Modules/AppPreferences/src/Sections/ThemePreferences/Model/ThemeModel.swift#L39
var selectedDarkThemeinThemeModel.swift::48- https://github.com/CodeEditApp/CodeEdit/blob/a0848b9f6385a8bb0fab05e01f0daf3b4a3c4d25/CodeEditModules/Modules/AppPreferences/src/Sections/ThemePreferences/Model/ThemeModel.swift#L48
var selectedThemeinThemeModel.swift::78- https://github.com/CodeEditApp/CodeEdit/blob/a0848b9f6385a8bb0fab05e01f0daf3b4a3c4d25/CodeEditModules/Modules/AppPreferences/src/Sections/ThemePreferences/Model/ThemeModel.swift#L78
func saveThemes()inThemeModel.swift::308- https://github.com/CodeEditApp/CodeEdit/blob/a0848b9f6385a8bb0fab05e01f0daf3b4a3c4d25/CodeEditModules/Modules/AppPreferences/src/Sections/ThemePreferences/Model/ThemeModel.swift#L308
To Reproduce
- Run CodeEdit
- Open a Project
- Open a file inside this Project
Expected behavior
Somewhere the state of selectionState is modified during view updates which should not happen. Further investigation is needed.
Version information
CodeEdit: 1.0 macOS: 13.0 Xcode: 14.1
Additional context
Screenshot
From what I can tell, this is a Xcode 14.x issue.
https://developer.apple.com/forums/thread/711899
https://www.donnywals.com/xcode-14-publishing-changes-from-within-view-updates-is-not-allowed-this-will-cause-undefined-behavior/
I have the same problem in one of my projects too and believe this is an issue with xcode itself
Closing this since it most likely is an issue with Xcode itself
I've had a look at this issue and may have found the cause of it.
-
var preferences in AppPreferencesModel.swift::35objectWillChange.send()is automatically fired by the@Publishedproperty wrapper, so it doesn't make sense to call it again. This will fire the updates twice, and update the view multiple times, as Xcode correctly reports. Removing theobjectWillChange.send()fixes this issue. -
var selectionState in WorkspaceDocument.swift::31WorkspaceSelectionStateis a Published struct which has a dict withObservableObjects in it. I don't think this is allowed, as this will also cause multiple view updates. EitherCodeFileDocumentwould need to become a struct (which doesn't seem possible, as it looks like some objc framework needs it), orWorkspaceSelectionStatewould need to become anObservableObjectclass. I gave this a shot and this seems to fix the issue, but causes issues with the TabBar, so I've just left this as is for now.
@Wouter01 can you provide a fix?
@Wouter01 can you provide a fix?
I've tried fixing it but ran into issues which I wasn't able to solve. As some of these classes will probably be extensively modified later in the development cycle, I'd just ignore it for now.
@Wouter01, @lukepistrol I think this issue should be re-opened because of the following Runtime Errors, including one in CETV:
