Sobes.tech
Junior — Senior

Detect errors in the image upload function

livecode

Task condition

This fragment of Swift code implements loading images from the network, but it contains several issues that can lead to memory leaks, incorrect UI update order, and other bugs. Analyze the code and identify all the shortcomings.

private func loadImage(){
    stories.forEach{ storyItem in
        if let bgUrl = URL(string: storyItem) {
            URLSession.shared.dataTask(with: bgUrl){ (data,_,_) in
                guard let imageData = data, let image = UIImage(data: imageData) else {return}
                self.storyImages.append(image)
            }.resume()
        }
    }
}