twostraws / Sourceful

A syntax highlighting source editor for iOS and macOS using UITextView and NSTextView.

Home Page:https://www.hackingwithswift.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

async dispatch causes loss of change (SwiftUI, macOS)

wtholliday opened this issue · comments

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?

parent.custom.didChangeText(parent)

@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