CodeEdit icon indicating copy to clipboard operation
CodeEdit copied to clipboard

🐞 Runtime Error: Publishing changes from within view updates

Open lukepistrol opened this issue 2 years ago • 6 comments

Description

When running the app and opening a file in a project, hundreds of runtime errors (purple) are generated on

  • var selectionState in WorkspaceDocument.swift::31
    • https://github.com/CodeEditApp/CodeEdit/blob/a0848b9f6385a8bb0fab05e01f0daf3b4a3c4d25/CodeEdit/Documents/WorkspaceDocument.swift#L31
  • var preferences in AppPreferencesModel.swift::35
    • https://github.com/CodeEditApp/CodeEdit/blob/a0848b9f6385a8bb0fab05e01f0daf3b4a3c4d25/CodeEditModules/Modules/AppPreferences/src/Model/AppPreferencesModel.swift#L35
  • var selectedLightTheme in ThemeModel.swift::39
    • https://github.com/CodeEditApp/CodeEdit/blob/a0848b9f6385a8bb0fab05e01f0daf3b4a3c4d25/CodeEditModules/Modules/AppPreferences/src/Sections/ThemePreferences/Model/ThemeModel.swift#L39
  • var selectedDarkTheme in ThemeModel.swift::48
    • https://github.com/CodeEditApp/CodeEdit/blob/a0848b9f6385a8bb0fab05e01f0daf3b4a3c4d25/CodeEditModules/Modules/AppPreferences/src/Sections/ThemePreferences/Model/ThemeModel.swift#L48
  • var selectedTheme in ThemeModel.swift::78
    • https://github.com/CodeEditApp/CodeEdit/blob/a0848b9f6385a8bb0fab05e01f0daf3b4a3c4d25/CodeEditModules/Modules/AppPreferences/src/Sections/ThemePreferences/Model/ThemeModel.swift#L78
  • func saveThemes() in ThemeModel.swift::308
    • https://github.com/CodeEditApp/CodeEdit/blob/a0848b9f6385a8bb0fab05e01f0daf3b4a3c4d25/CodeEditModules/Modules/AppPreferences/src/Sections/ThemePreferences/Model/ThemeModel.swift#L308

To Reproduce

  1. Run CodeEdit
  2. Open a Project
  3. 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

Screenshot 2022-10-29 at 03 42 44

lukepistrol avatar Oct 29 '22 01:10 lukepistrol

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/

matthijseikelenboom avatar Nov 04 '22 14:11 matthijseikelenboom

I have the same problem in one of my projects too and believe this is an issue with xcode itself

pabloscloud avatar Nov 04 '22 14:11 pabloscloud

Closing this since it most likely is an issue with Xcode itself

lukepistrol avatar Dec 05 '22 10:12 lukepistrol

I've had a look at this issue and may have found the cause of it.

  • var preferences in AppPreferencesModel.swift::35 objectWillChange.send() is automatically fired by the @Published property 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 the objectWillChange.send() fixes this issue.

  • var selectionState in WorkspaceDocument.swift::31 WorkspaceSelectionState is a Published struct which has a dict with ObservableObjects in it. I don't think this is allowed, as this will also cause multiple view updates. Either CodeFileDocument would need to become a struct (which doesn't seem possible, as it looks like some objc framework needs it), or WorkspaceSelectionState would need to become an ObservableObject class. 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 avatar Jan 10 '23 21:01 Wouter01

@Wouter01 can you provide a fix?

lukepistrol avatar Jan 10 '23 22:01 lukepistrol

@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 avatar Jan 11 '23 05:01 Wouter01

@Wouter01, @lukepistrol I think this issue should be re-opened because of the following Runtime Errors, including one in CETV: shot shot 1 shot 2 shot 3

luah5 avatar Apr 12 '23 17:04 luah5