UITableView Reload Not Updating the tableview with DispatchQueue.main.async

I have written code with swift4 and running on iPhone simulator.

DispatchQueue.main.async {
     self.view.activityindicatorStop()
     self.mytableView.reloadData()
}

Above code has following operation

Stoping Activity indicator and update TableView after data filling with array..

But expected does not happen..

I got following

  1. Activity indicator stoped but indicator not removed and background view not removed.
  2. TableView not updated.

code i have used:

 super.viewDidLoad()


        UserDefaults.lastAccessDate = Date()


        self.view.activityStartAnimating(activityColor: #colorLiteral(red: 0.9176470588, green: 0.2901960784, blue: 0.262745098, alpha: 1), backgroundColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0))

       //Calling the API  
      Capsulle.retriveSpaceTimeCapsulle { (data, respose, error) in
        if nil == error
        {
         let ServerData = serverComm.toJSONObject(with: data!)
         let result = ServerData.0 as? Array<Dictionary<String, Any>>
         self.capsulleDetail = result

        if (result == nil)
        {
            if false ==  loginCredential.isHavingAccessToken
            {
            loginCredential.isHavingAccessToken = false;
            UserDefaults.standard.set( "", forKey: "UserEmailID")
            let objStoryBoard:UIStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
            let initialViewController:UIViewController = objStoryBoard.instantiateViewController(withIdentifier: "loginVC")

            self.present(initialViewController, animated: true, completion: {

            })

            return
            }
        }


         DispatchQueue.main.sync {

            self.CapsulleCollectionView.reloadData()

            self.view.activityStopAnimating()

         }
         }

      }

    }

func activityStartAnimating(activityColor: UIColor, backgroundColor: UIColor) {
        let backgroundView = UIView()
        backgroundView.frame = CGRect.init(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height)
        backgroundView.backgroundColor = backgroundColor
        backgroundView.tag = 475647

        var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
        activityIndicator = UIActivityIndicatorView(frame: CGRect.init(x: 0, y: 0, width: 50, height: 50))
        activityIndicator.center = self.center
        activityIndicator.hidesWhenStopped = true
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
        activityIndicator.color = activityColor
        activityIndicator.startAnimating()
        self.isUserInteractionEnabled = false

        backgroundView.addSubview(activityIndicator)

        self.addSubview(backgroundView)
    }

func activityStopAnimating() {
    if let background = self.viewWithTag(475647){

        background.removeFromSuperview()
    }
    self.isUserInteractionEnabled = true
}

i have created activityStopAnimating and activityStartAnimating as extensions

For tableView to reload you have to make sure that self.capsulleDetail have data changed if this array is your data source i think it not changed or still zero so reloadData() will do nothing

To hide indicator you should set this property to hide it when stop or hide it directly by sting is-hidden to true

self.activityIndicator.hidesWhenStopped = true

ios, DispatchQueue.main.async { // now update UI on main thread self.myTableView.​reloadData() self.myTableView.setContentOffset(CGPoint(x: 0, y: -1), animated:  Questions: I’ve written two ways to async load pictures inside my UITableView cell. In both cases the image will load fine but when I’ll scroll the table the images will change a few times until the scroll will end and the image will go back to the right image.

DispatchQueue.main.async {
    self.view.activityIndicator.stopAnimating()
    self.view.activityIndicator.isHidden = true
    self.mytableView.beginUpdates()
    self.mytableView.endUpdates()
}

your code should work for tableView reload but you can also try using beginUpdates() and endUpdates() method. and to stopActivityindicator call stopAnimating() and then hide your activityindicator this code should work for you

Enjoy coding.

Whats the proper way to modify a tableview from an async network , This had a weird lag since it wasnt called on the main thread, so I added a dispatchqueue.main.sync{} around the reload tableview. code TheCompanyAPISession  It adjusts offsets if the table shrinks as a result of the reload. The table view’s delegate or data source calls this method when it wants the table view to completely reload its data. It should not be called in the methods that insert or delete rows, especially within an animation block implemented with calls to begin Updates() and end

I don't Find any correct solutions to do this perfectly..

after long run i decided to do following and it is working some what i expect..

DispatchQueue.main.async {
                        // now update UI on main thread

                        self.myTableView.reloadData()
                        self.myTableView.setContentOffset(CGPoint(x: 0, y: -1), animated: true)
                        self.view.activityStopAnimating()

                    }

Thanks for all of your answers..i am awaiting for perfect answer..

Tableview not reloading |Apple Developer Forums, My tableview does not reload data even though I call tableview.reloaddata(). Maybe you are calling `reload()` in non-main thread, or you may be calling func tableView(_ tableView: UITableView, cellForRowAt indexPath: { (action) -> Void in; // UPDATE data; DispatchQueue.main.async {; self. This article has been updated for Swift 3. One of the most versatile UI elements which you come across in iOS development is UITableView.With a combination of custom cell and animation its possibilities are endless.From fancy menu to image scroll you must have found it at many places.

Table View Not Reloading from texfield in Parent View, Table View Not Reloading from texfield in Parent View - Swift 3 I've tried updating the tableview on the Main Thread but that does not seem to work. weak var tableView: UITableView! var businessSearch = [Business]() var businessSearch = tmp DispatchQueue.main.async(execute: { () -> Void in self. These method pairs can be nested. If you do not make the insertion, deletion, and selection calls inside this block, table attributes such as row count might become invalid. You should not call reload Data() within the group; if you call this method within the group, you must perform any animations yourself.

`reloadData()` doesn't reflect the latest data source · Issue #21 , I change some settings and try to refresh the spreadsheetView with the usual: spreadsheetView. doing the same thing with UITableView will also produce unexpected behavior. is was a tableView and trying to refresh it outside of the func does not work. DispatchQueue.main.async { self.ssview. With this change, our code is both better and worse. It's better because it no longer blocks the main thread while the JSON downloads from Whitehouse.gov. It's worse because we're pushing work to the background thread, and any further code called in that work will also be on the background thread

Back to the main thread: DispatchQueue.main, It's better because it no longer blocks the main thread while the JSON parsed on the background thread and the table view's reloadData() will be called on the We could modify our code to have async() before every call to showError() and​  Hi. I have a case where I need to reload the cells because the value of certain fields may change (price, mood, etc). I do not want to do a full reload and I want to keep the currently viewed cells in place - I just need to trigger the currently rendered cells to update/re-populate their view based on the latest and greatest values that are stored in the backing list - I already keep a

Comments
  • you will have to show all the code behind activityindicatorStop()
  • Add breakpoint and check once again the code has not issue, it might be of linking tableview and delegate and UITableViewDataSource.
  • Share your code
  • @MilanNosáľ tried same thing happened ..
  • You did not show us the relevant code.
  • Are you make sure that self.capsulleDetail changed
  • yeah value get updated..actual thing is UI not getting updated..if i touch any where in the screen the updated UI showing to me..
  • Are you connect CollectionView to IBoutlet
  • yes..Connected ..if not view does not appear right?
  • sorry if i do scroll (like pull to refresh) ...it will work
  • it will send exceptions... actually begin and end update used for insert or delete right?
  • yes you are right it is used when we insert or delete but you when we call begin and endupdate it re-excecute all the tableview methods so your data will be reloaded and i think you should try this may be it will work for you.