Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DiffUtil

 DiffUtil

About DiffUtil in Kanmoba #17

Takuji Nishibayashi

August 31, 2016
Tweet

More Decks by Takuji Nishibayashi

Other Decks in Technology

Transcript

  1. ܭࢉͰ͖Δมߋ • ௥Ճ • ࡟আ • ߋ৽ • Ҡಈ •

    Φϓγϣϯࢦఆ • ܭࢉίετ͕௓Ͷ্͕Δ
  2. Model enum class Status { INTERESTED, LIKE, LOVE; } data

    class Artist(val name: String, val status: Status) { companion object { val list: List<Artist> = listOf( Artist(name = "খ૔།", status = LOVE), Artist(name = "Ӎٶఱ", status = LOVE), //... ) } }
  3. DiffUtil.Callback class DiffCallback( val oldList: List<Artist>, val newList: List<Artist>) :

    DiffUtil.Callback() { override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { return oldList[oldItemPosition].name == newList[newItemPosition].name } override fun getOldListSize(): Int { return oldList.size } override fun getNewListSize(): Int { return newList.size } override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { return oldList[oldItemPosition] == newList[newItemPosition] } override fun getChangePayload( oldItemPosition: Int, newItemPosition: Int): Pair<Status, Status> { return Pair(oldList[oldItemPosition].status, newList[newItemPosition].status) } }
  4. diffΛܭࢉ͢Δ fun updateItems(items : List<Artist>) { val oldItems = adapter.items

    val diffResult = DiffUtil.calculateDiff( DiffCallback( oldList = oldItems, newList = items ), true ) adapter.items = items diffResult.dispatchUpdatesTo(adapter) }
  5. ੍ݶࣄ߲ • ৽͍͠Listͱݹ͍ListΛൺֱ͢Δ • × MutableͳList • RealmCollection • ×

    ཁૉͦͷ΋ͷ͕ߋ৽͞ΕΔ • RealmObject • Listͷཁૉ਺͸2^26(=67108864)·Ͱ