Search code examples
iosswift4xcode9uiactivityindicatorview

ui activity indicator is not showing up in viewcontroller, in which it has table view and web view?


var actInd: UIActivityIndicatorView = UIActivityIndicatorView()
 actInd.frame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0)
   actInd.center = view.center
actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
 view.addSubview(actInd)
getinfo()
actInd.startAnimating()
func getinfo(){
 //in this function theres is few image parsing codes, for table view

for i in 0 ..< minuscurent1.count
    {
        let adict:NSDictionary = minuscurent1[i] as! NSDictionary

        let bdict:NSDictionary = adict.object(forKey: "snippet") as! NSDictionary
        let cdict:NSDictionary = bdict.object(forKey: "resourceId") as! NSDictionary
        let ddict:NSDictionary = bdict.object(forKey: "thumbnails") as! NSDictionary
        let edict:NSDictionary = ddict.object(forKey: "medium") as! NSDictionary

        self.videolist.add(bdict.object(forKey: "title") as! String)
        self.videoId.add(cdict.object(forKey: "videoId") as! String)
        self.image.add(edict.object(forKey: "url") as! String)
    }
DispatchQueue.main.async {
            self.actInd.stopAnimating()
            self.actInd.hidesWhenStopped = true
            self.videotable.reloadData()
        }

i want the activity indicator to start from beginning and end it when all the images and web view is loaded.but the activity indicator is not showing up.i don't know what is wrong with the code.any help is appreciated...


Solution

  • Your image-loading is happening in the main-thread, thus blocking the UI from updating. You have to do the heavy work in a background thread and only update the activity-indicator once youre done.

    DispatchQueue.global(qos: .background).async {
        // Background Thread
        // put your image loading here (for-loop)
    
    
        DispatchQueue.main.async {
            // Run UI Updates
            self.actInd.stopAnimating()
            self.actInd.hidesWhenStopped = true
            self.videotable.reloadData()
        }
    }