1

I'm new to Swift development, and I'm completely lost right now. I made a simple label and placed it on the Storyboard. None of my code interacts with it at all, yet it takes 8-30 seconds to load, unlike the other UILabels that were placed there by default. Why is this?

I have the same issue with an image which gets pulled from a URL. It immediately downloads the image and assigns it to the UIImageView, but it takes 8-30 seconds to actually update, and I have no idea why.

Here is my controller's code FirstViewController.swift

import UIKit

class FirstViewController: UIViewController {
    @IBOutlet var profilePicture: UIImageView!
    var currentUser: User!
    @IBOutlet var profileText: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Declare currentUser variable
        currentUser = User(email: "", first_name: "", last_name: "", department: "", admin: false, superadmin: false, thumb_url: "")

        // Load timesheet structs
        getTimesheetPages()

        // Load user struct and wait for it to finish
        getUserDetails() {
            self.downloadProfilePic()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func downloadProfilePic() {
        let regURL = "http://localhost:3000"
        let profilePicURL = URL(string: regURL + currentUser.thumb_url)!

        let session = URLSession(configuration: .default)

        // Define a download task
        let downloadPicTask = session.dataTask(with: profilePicURL) { (data, response, error) in
            // The download has finished.
            if let e = error {
                print("Error downloading profile picture: \(e)")
            } else {
                // No errors found.
                if let res = response as? HTTPURLResponse {
                    print("Downloaded profile picture with response code \(res.statusCode)")
                    if let imageData = data {
                        print("Enters image load")

                        // Finally convert that Data into an image and do what you wish with it.
                        let image = UIImage(data: imageData)

                        // Do something with your image.
                        self.profilePicture.image = image

                        // Initialize profile picture properties
                        self.profilePicture.layer.borderWidth = 1.0
                        self.profilePicture.layer.masksToBounds = false
                        self.profilePicture.layer.borderColor = UIColor.white.cgColor
                        self.profilePicture.layer.cornerRadius = self.profilePicture.frame.size.width/2
                        self.profilePicture.clipsToBounds = true

                    } else {
                        print("Couldn't get image: Image is nil")
                    }
                } else {
                    print("Couldn't get response code for some reason")
                }
            }
        }

        downloadPicTask.resume()
    }

// more functions

Here is an image of what I'm working with Image of Storyboard

Here is what my emulator displays for the first ~10 seconds Image of Emulator

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Holabola
  • 143
  • 1
  • 3
  • 11

1 Answers1

2

As Leo said, I was wrapping my entire function calls in DispatchQueue.main.async { } instead of just the UI update calls.

By changing self.profilePicture.image = image to

DispatchQueue.main.async { 
    self.profilePicture.image = image
    // rest of UI update code 
}

it worked flawlessly

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Holabola
  • 143
  • 1
  • 3
  • 11