Подлагивание при скроллинге таблицы

Пытаюсь сделать подгрузку данных. Добавляю ячейки в UITableViewController через insertRows, однако при их добавлении наблюдается кратковременный лаг. Попытался записать, но на видео не очень заметно: https://gfycat.com/ru/ediblepoisedapatosaur

let currentCount = self.notifications.count
let newData = result.data!.return!
var newDataCount = 0
self.notificationsTable.beginUpdates()
self.notificationsTable.deleteRows(at: [IndexPath(row: currentCount, section: self.readedSection)], with: .none) // удаляю ячейку с UIActivityIndicator
self.notificationsTable.insertRows(at: newData.map {notification in
  let indexPath = IndexPath(row: currentCount + newDataCount, section: self.readedSection)
  newDataCount += 1
  return indexPath
}, with: .none)
self.notificationsTable.endUpdates()
self.viewWillLayoutSubviews()

override func viewWillLayoutSubviews() {
  notificationsTableHeight.constant = notificationsTable.contentSize.height
  super.updateViewConstraints()
  super.viewWillLayoutSubviews()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  if notifications.count == 0 && unreadNotifications.count == 0 {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "NotAvailableCell", for: indexPath) as? NotAvailableCell {
      cell.label.text = "Нет новых уведомлений"
      return cell
    }
  } else if indexPath.section == readedSection && notifications.count > 0 && notifications.count <= indexPath.item {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "LoadingCell", for: indexPath) as? LoadingCell {
      cell.spinner.startAnimating()
      return cell
    }
  } else {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "NotificationCell") as? NotificationCell {
      if indexPath.section == unreadedSection {
        cell.notification = unreadNotifications[indexPath.item]
      } else if indexPath.section == readedSection {
        cell.notification = notifications[indexPath.item]
      }
      return cell
    }
  }
  return UITableViewCell()
}

Как это подвисание можно пофиксить?


Ответы (2 шт):

Автор решения: WGPavell

Убрал UIScrollView, в котором находилась таблица и теперь ситуация стала лучше. Однако, при скроллинге заметен "скачок" ячеек и чем ниже запускаешься, тем он сильнее.

→ Ссылка
Автор решения: K.S

Попробуйте проверить cell.spinner.startAnimating(). Возможно что то в главном потоке срабатывает. Попробуйте привязать анимацию к частоте обновления экрана. Но с этим надо быть аккуратным.

→ Ссылка