Skip to content

Latest commit

ย 

History

History
24 lines (15 loc) ยท 1.27 KB

diff_util.md

File metadata and controls

24 lines (15 loc) ยท 1.27 KB

DiffUtil

DiffUtil์ด๋ž€?

DiffUtil์€ ์„œ๋กœ ๋‹ค๋ฅธ ์•„์ดํ…œ์ธ์ง€๋ฅผ ์ฒดํฌํ•˜์—ฌ ๋‹ฌ๋ผ์ง„ ์•„์ดํ…œ๋งŒ ๊ฐฑ์‹ ์„ ๋„์™€์ฃผ๋Š” Util๋กœ RecyclerView์˜ ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค. Eugene W. Myers์˜ ์ฐจ๋ถ„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•œ ๋ชฉ๋ก์„ ๋‹ค๋ฅธ ๋ชฉ๋ก์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•œ ์ตœ์†Œ ์—…๋ฐ์ดํŠธ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

DiffUtil.ItemCallback

ListAdapter์˜ DiffUtil.ItemCallback์€ areItemsTheSame๊ณผ areContentsTheSame ๋‘ ๋ฉ”์†Œ๋“œ๋งŒ ์ƒ์†๋ฐ›์•„ ๊ตฌํ˜„ํ•˜์—ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ DiffUtil.ItemCallback๋งŒ ์•Œ์•„๋ณด๋„๋ก ํ•œ๋‹ค.

 public abstract static class ItemCallback<T> {
      
        public abstract boolean areItemsTheSame(@NonNull T oldItem, @NonNull T newItem);

       
        public abstract boolean areContentsTheSame(@NonNull T oldItem, @NonNull T newItem);
    }
  • areItemsTheSame : ํ˜„์žฌ ๋ฆฌ์ŠคํŠธ์— ๋…ธ์ถœํ•˜๊ณ  ์žˆ๋Š” ์•„์ดํ…œ๊ณผ ์ƒˆ๋กœ์šด ์•„์ดํ…œ์ด ์„œ๋กœ ๊ฐ™์€์ง€ ๋น„๊ต๋ฅผ ์œ„ํ•ด ๊ณ ์œ ํ•œ ID ๊ฐ’๋“ฑ์„ ์ฒดํฌ
  • areContentsTheSame : areItemsTheSame์ด false์ผ ๊ฒฝ์šฐ ํ˜ธ์ถœ๋˜๋ฉฐ, ํ˜„์žฌ ๋ฆฌ์ŠคํŠธ์— ๋…ธ์ถœํ•˜๊ณ  ์žˆ๋Š” ์•„์ดํ…œ๊ณผ ์ƒˆ๋กœ์šด ์•„์ดํ…œ์˜ equals๋ฅผ ๋น„๊ต

equals๋ฅผ ๋น„๊ตํ•  ๋•Œ Kotlin data class๋ฅผ ๊ผญ ํ™œ์šฉํ•˜๋„๋ก ํ•˜์ž.