Swift UITableView reloadData in a closure

前端 未结 6 614
梦如初夏
梦如初夏 2020-12-03 02:25

I believe I\'m having an issue where my closure is happening on a background thread and my UITableView isn\'t updating fast enough. I am making a call to a REST service and

相关标签:
6条回答
  • 2020-12-03 02:58

    You can also reload UITableView like this

    self.tblMainTable.performSelectorOnMainThread(Selector("reloadData"), withObject: nil, waitUntilDone: true)
    
    0 讨论(0)
  • 2020-12-03 02:59

    SWIFT 3:

    OperationQueue.main.addOperation ({
         self.tableView.reloadData()
    })
    
    0 讨论(0)
  • 2020-12-03 03:07

    You can also update the main thread using NSOperationQueue.mainQueue(). For multithreading, NSOperationQueue is a great tool.

    One way it could be written:

    NSOperationQueue.mainQueue().addOperationWithBlock({
         self.tableView.reloadData()       
    })
    

    Update: DispatchQueue is the way to go for this

    0 讨论(0)
  • 2020-12-03 03:18

    UIKit isn't thread safe. The UI should only be updated from main thread:

    dispatch_async(dispatch_get_main_queue()) {
        self.tableView.reloadData()
    }
    

    Update. In Swift 3 and later use:

    DispatchQueue.main.async {
        self.tableView.reloadData()
    }
    
    0 讨论(0)
  • 2020-12-03 03:22

    With Swift 3 use

    DispatchQueue.main.async {
        self.tableView.reloadData()
    }
    
    0 讨论(0)
  • 2020-12-03 03:24
    DispatchQueue.main.async {
        self.tableView.reloadData()
    }
    

    to reflect in UI the data should be in main Thread. So, usage of this method will bring the data to main thread and it will be make available to reflect in UI.

    0 讨论(0)
提交回复
热议问题