使用 RxTableViewSectionedAnimatedDataSource 但卻不想要動畫

RxDataSources 分為 RxTableViewSectionedReloadDataSource 與 RxTableViewSectionedAnimatedDataSource,除了有沒有動畫的差別外,Animated data source 會根據 identity 的不同來判斷列表需要插入、重整或是刪除,適合分批載入資料或是有新增移除需求的列表使用。

但是如果我們想要有 Animated data source 的演算法特性,又不希望列表在變動的時後有動畫呢?設置 dataSource 裡的 animationConfiguration 是行不通的:

dataSource.animationConfiguration = AnimationConfiguration(insertAnimation: .none, reloadAnimation: .none, deleteAnimation: .none)

因為弔詭的是,.none 並不是沒有動畫,而是使用預設動畫 : [

case none

The inserted or deleted rows use the default animations.

相關的討論可以參考這裡

Workaround

研究許久後,目前有一種 workaround 的方式,就是在更新 section model 資料的時候開一個主線程再加上 UIView.performWithoutAnimation {}

DispatchQueue.main.async {
   UIView.performWithoutAnimation {
      // update your section model data here
   }
}