Sourceful
Sourceful copied to clipboard
async dispatch causes loss of change (SwiftUI, macOS)
https://github.com/twostraws/Sourceful/blob/04f7e85900451ebdc34345ee19bb0139f05f3a01/Sources/Sourceful/SwiftUI/SourceCodeTextEditor.swift#L139
This line causes an occasional loss of a change with SwiftUI on macOS, presumably due to reordering of updates. After typing a character, the change is undone.
Why was the line needed? There isn't a comment in the code or an explanation in the change description. On macOS, SyntaxTextView.Coordinator.didChangeText is already called on the main thread.
There's this clue: // allow the client to decide on thread
... but I'm still not sure the intent.
@andreweades
thanks!
I forget what this line does exactly, but it without it I’ve seen really odd issues in my text editor on iOS at least. I wouldn’t remove the code, maybe add a check for macOS or iOS and run the code when on iOS. This is safer than just removing the call completely.
Well I've got to support iOS too, so I'll try to get to the bottom of it. I think it's pretty strange to be doing async stuff there.
Okay after some looking I found the commit that added this for my editor. I believe the reason for it was an issue with SwiftUI crashing when it was not accessed from the main thread. Which can sometimes occur.
Ok, what about using DispatchQueue.main.sync
instead of async
? That should avoid the reordering. I'll probably give that a shot soon.
Also, why would I want a callback on another thread? https://github.com/twostraws/Sourceful/blob/04f7e85900451ebdc34345ee19bb0139f05f3a01/Sources/Sourceful/SwiftUI/SourceCodeTextEditor.swift#L144
@ActuallyZach , do you know the circumstances under which didChangeText
is called on a thread other than the main thread? I'm testing on iOS and just see it being called on the main thread:
![image](https://user-images.githubusercontent.com/841144/134543340-c9bbc6b7-3bbf-4c58-b644-4fc550ff148b.png)