title: Get Error "Index out of range" When I frequently push/back viewcontroller in real device - Swift
body: <p>I have a problem about my tableView.<br>
  When  I frequently push/back between ChatListViewController and detailViewcontroller, I will crash and get error in ListViewController.<br>
  But I don't know where my data make it crash.<br>
  I guess Whether I use GCD to make it happen or not?<br>
  Have any advice to avoid it?<br>
  Thanks.<br></p>
  
  <p>Crash Log:<br>
      fatal error: Index out of range
      <img src="https://i.stack.imgur.com/N32Io.png" alt="![enter image description here">
      <img src="https://i.stack.imgur.com/10oWc.png" alt="![enter image description here"></p>
  
  <p>Model: </p>
  
  <pre><code>class ChatroomList:Model {
  
      var all:[Chatroom] {
      var rooms:[Chatroom] = [Chatroom]()
      self.chatrooms.forEach({ (id,chatroom) in
          if showType.contains(chatroom.type) {
              rooms.append(chatroom)
          }
      })
      return rooms
      }
  }
  </code></pre>
  
  <p>ViewController:</p>
  
  <pre><code>import RxCocoa
  import RxSwift
  import Alamofire
  
  class ListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {                     
  
      let chatrooms:ChatroomList = ChatroomList()
      var list:[Chatroom] = [Chatroom]()
      var subscribe:Disposable?
  
      override func viewDidLoad() {
      super.viewDidLoad()
  
      self.tableView.dataSource = self
      self.tableView.delegate = self
  
      }
  
      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
  
          subscribe = rooms.notifySubject.subscribe({ json in
                  self.loadContents()
          })
          self.loadContents()
      }
  
      override func viewWillDisappear(_ animated: Bool) {
          super.viewWillDisappear(animated)
  
          subscribe?.dispose()
      }
  
      func loadContents() {
  
          var idList:[String] = []
  
          self.list.removeAll()
          self.list = chatrooms.all
  
          guard self.list.isEmpty == false else {
              return
          }
  
          DispatchQueue.global().async() {
  
              self.list = self.list.filter { (chatroom) -&gt; Bool in
                  if chatroom.id.isEmpty {
                      return true
                  }
                  if idList.contains(chatroom.id) {
                      return false
                  }
                  idList.append(chatroom.id)
                  return true
              }   
  
              self.list.sort(by: { (a,b) in
                  if a.message.datetime.isEmpty {
                      return false
                  }
  
              return a.message.datetime &gt; b.message.datetime
          })
  
          DispatchQueue.main.async() {
  
              self.tableView.reloadData()
          }
        }
      }
  }
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return list.count
  }
  
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
  
      if list[indexPath.row].type == .city {
  
          let cell: ChatroomCityTableViewCell = ChatroomCityTableViewCell(style: .default, reuseIdentifier: nil)
  
          cell.loadByCityChatroom(chatroom: list[indexPath.row], cityId: list[indexPath.row].cityId)
  
          return cell
      }else{
  
          let cell: ChatroomTableViewCell = ChatroomTableViewCell(style: .default, reuseIdentifier: nil)
          cell.loadByChatroom(chatroom: list[indexPath.row])
  
          return cell
      }
  }
  </code></pre>