|
It seems that forum posts in the classic "100 days of Swift" forum don't get many responses. But still, I'll try again. I'm trying to follow the instructions for Project9, where we are basically modifying our code from Project7 to use threading. I believe I have followed all the instructions to modify the code as Paul suggested, but now I am getting 2 purple warnings about things that should only be used in the main thread when I try to run my code. My final code for my ViewController class looks like this...
The warnings I'm getting are in this segment in the
and this line inside the
I just don't know if this is something that I have done wrong, or something that I should just ignore and move on. |
|
Can you confirm if putting the problem code inside DispatchQueue.main.async , still gives error, its important to not ignore this, the main thread is where any code for UI change must operate as per compiler , so you get this error, you can use DispatchQueue.main.async to ensure code is on main thread ...
|
|
I could do that, but that kind of defeats the purpose of calling the So, to get rid of the warning completely, I would probably have to move the code that checks which tab is currently selected into its own function that runs on the main thread and returns the appropriate URL, and then pass the URL into the |
|
@FlyOstrich has the Purple bug:
I will be honest: I have not thoroughly reviewed your code. But this part in the
The warning tells you that this can only run on the Main thread, yet you're calling it in a background thread.
Think of an extreme case. You want to fetch JSON, so you call this method and put it in the background. Maybe it takes 3 hours before that background thread gets executed. In the meantime, your user has selected a different Well, when should the background thread make this decision? When the |
|
I understand the reasons that Xcode gives the warnings, and I know how to make them go away. But I guess I was just wondering why I get the warnings when I followed the instructions from the project. Like, if I was missing something, or if there was a reason why Paul showed us to do it the way that he did rather than the correct way. Or perhaps if there is just a bug in Xcode that causes the warnings to show even though they are actually irrellevant in these cases. Anyway, I modified the code like this, and now the errors don't show. But it is quite a bit different from the code that Paul provided for the project now.
|
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.