[ad_1]
In a MacOS app, I have a callback function called by another module (which is a network module that gets some response from the Internet to fire the callback):
func callbackHandler()
someViewController.updateSomeView()
In this callback handler, a view controller is called to update some view, and inside the view controller, reload data for a table:
func updateSomeView()
someTable.reloadData()
However, by doing so, I will get an error like this:
Main Thread Checker: UI API called on a background thread: ...
Therefore, I have to add DispatchQueue.main.async()
either in the caller :
func callbackHandler()
DispatchQueue.main.async()
someViewController.updateSomeView()
or the callee:
func updateSomeView()
DispatchQueue.main.async()
someTable.reloadData()
Either way will solve the problem. But I think it's quite weird for the caller or the callee to be aware of that UI API called is going to be called on a background thread by adding DispatchQueue.main.async()
as a fix.
How to do it in a proper way in terms of design architecture?
[ad_2]
لینک منبع