|
In my project I load JSON data from a server and display it in a list. I have a class NetworkManager which handles the POST, GET and DELETE tasks. Inside .onAppear() of the MeterView Struct I load the data from the server.
When I start the app it works at first with no apparent problems, however I see the following error message in the console (Xcode) which refers to the "networkManager..." line:
I found the following article by Paul on Hacking with Swift: How to use @MainActor to run code on the main queue and tried various things, but unfortunately without success. I must confess I don't know how to prevent the message. I have tried using a Task in .onAppear - no success:
Can anyone give me a tip on how to prevent the message and thus improve the code? |
|
Try this:
Or switch to using |
|
|
|
It would be nicer to figure out how to do it with async/await instead of GCD. Try marking your NetworkManager class @MainActor. |
|
Hi @bobstern, after reading Paul's post about @MainActor my first attempt was to mark the NetworkManager class as @MainActor. Unfortunately after that the message in Xcode about the above mentioned line was still displayed .... ??!! After that I wrote the article here in the forum and after roosterboy's change Xcode didn't give a warning anymore. I do not know what I did wrong regarding @MainActor or maybe that alone was not enough. |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.