Sourceful icon indicating copy to clipboard operation
Sourceful copied to clipboard

async dispatch causes loss of change (SwiftUI, macOS)

Open wtholliday opened this issue 2 years ago • 5 comments

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!

wtholliday avatar Sep 19 '21 16:09 wtholliday

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.

ActuallyTaylor avatar Sep 19 '21 17:09 ActuallyTaylor

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.

wtholliday avatar Sep 19 '21 18:09 wtholliday

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.

ActuallyTaylor avatar Sep 20 '21 12:09 ActuallyTaylor

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

wtholliday avatar Sep 20 '21 19:09 wtholliday

@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

wtholliday avatar Sep 23 '21 16:09 wtholliday