In my app I import a JSON file and display its content in the app. In order to avoid freezing of the UI, because the file can be bigger sometimes, I parse the file in a background thread and convert it into my internal swift structure. When the parsing is done, the view should be redrawn, as the content of the structure has changed. Because I mustn't change the view from a background thread I have some kind of workaround at the moment.
I start the parsing in the action of the .fileImporter modifier of a button like this:
DispatchQueue.global(qos: .userInitiated).async {
documentManager.parseInput()
}
And in the parseInput method in the documentManager I do something like this
func parseInput() {
DispatchQueue.main.async { [self] in
inputIsParsed = false
}
// parse ...
DispatchQueue.main.async { [self] in
inputIsParsed = true
}
}
inputIsParsed is a @Published variable in the documentManager, which is my ViewModel
@Published var inputIsParsed = false
My question now is: Is this as bad as I think it is or is there no better way that probably also involves a completely different concept of putting work into the background and redrawing the view when the calculation has finished?