Avatar ·

How to properly execute code in a background thread in Swift (Grand Central Dispatch)?

Hi everyone! I'm a beginner in iOS development with Swift, and I'm having a problem with multithreading. I have an app that downloads images from the internet. When I call the loading function on the main thread, the interface freezes for a few seconds — the table scroll lags, animations disappear. I understand that I need to move heavy operations (network requests, data processing) to a background thread, but I don't know how to do it correctly. Here's an example of my code: ```swift func loadImage(from urlString: String) -> UIImage? { guard let url = URL(string: urlString) else { return nil } do { let data = try Data(contentsOf: url) return UIImage(data: data) } catch { print("Error loading image: \(error)") return nil } } // Usage