๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[Android ์‚ฝ์งˆ ๊ตํ›ˆ] RecyclerView์— RxJava๋Š” ์“ฐ์ง€ ๋ง์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋ฆฌ์‚ฌ์ดํด๋Ÿฌ๋ทฐ์˜ ๋ฆฌ์ŠคํŠธ ์•„์ดํ…œ์— ์—ฐ์†๋˜๋Š” ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ๋ง‰๊ณ ์ž RxJava์˜ throttleFirst๋ฅผ ํ™œ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

(์ €๋Š” ์ด ์ž‘์—…์ด RxJava์˜ ์‘์šฉ์— ์ œ๊ฐ€ ํ•œ ๋ฐœ์ง ๋” ๋‚˜์•„๊ฐ”๋‹ค๋Š” ์ƒ๊ฐ์— ์‚ฌ๋ฌด์‹ค์—์„œ ๋‚ด์  ๊ธฐ์จ์„ ๋ˆ„๋ ธ์–ด์š”.)

 

๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋„ ์žŠ์ง€ ๋ง๊ณ  ๋ง‰์•„์•ผ์ง€๐Ÿ˜‰ ์ด๋Ÿฌ๊ณ   RecyclerView์—์„œ RxJava์˜ dispose๋ฅผ ์ง„ํ–‰ํ•  ์ ๋‹นํ•œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‚˜ ๊ตฌ๊ธ€์—๊ฒŒ ๋ฌผ์–ด๋ดค์Šต๋‹ˆ๋‹ค.

StackOverFlow์—์„œ ์–ด๋Œ‘ํ„ฐ ์•ˆ์˜ onViewDetachedFromWindow๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค๋Š” ๊ธ€์„ ํ™•์ธํ–ˆ๊ตฌ์š”.

 

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์งฐ์–ด์š”.

override fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder) {
    if (holder is ViewHolder) {
        holder.disposable.dispose()
    }
    super.onViewDetachedFromWindow(holder)
}

 

๊ทธ๋ฆฌ๊ณ  ์ด์Šˆ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜ง

ํ•ด๋‹น RecyclerView์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์Šคํฌ๋กคํ•ด์„œ ์ž ๊น ๊ฐ€๋ ค์กŒ๋‹ค๊ฐ€ ๋‹ค์‹œ ๋ณด์ด๊ฒŒ ๋œ ๋ฆฌ์ŠคํŠธ ์•„์ดํ…œ์—์„œ๋Š” ํด๋ฆญ ์ด๋ฒคํŠธ๊ฐ€ ๋จนํžˆ์งˆ ์•Š์•˜์Šต๋‹ˆ๋‹ค.

 

RecyclerView์˜ ์•„์ดํ…œ์ด ํ™”๋ฉด์— ๋…ธ์ถœ๋˜์ง€ ์•Š์•˜์„ ๋•Œ onViewDetachedFromWindow()๊ฐ€ ํ˜ธ์ถœ๋˜์—ˆ๊ธฐ์— ์ด๋ฏธ dispose() ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜์—ˆ๋˜ ๊ฒƒ์ด์˜€์–ด์š”. ๊ฐ€๋ ค์ง„ ๊ทธ ์•„์ดํ…œ์ด ๋‹ค์‹œ ํ™”๋ฉด์— ๋…ธ์ถœ๋˜์–ด ์ƒ์„ฑ๋œ ํด๋ฆญ ์ด๋ฒคํŠธ ์ŠคํŠธ๋ฆผ์€ ๊ทธ ๋ˆ„๊ตฌ๋„ ๋ฐ›์•„์ฃผ์ง€ ์•Š๊ณ  ํ˜๋Ÿฌ๊ฐ€๊ฒŒ ๋˜์—ˆ๋˜ ๊ฑฐ์˜€์–ด์š”.... ๐Ÿ˜ข(๋ˆˆ๋ฌผ ๋˜๋ฅด๋ฅด)

 

์ž ์‹œ ๋ฐ˜์„ฑ์˜ ์‹œ๊ฐ„์„ ๊ฐ€์ง€๊ณ ...๐Ÿ˜ž onViewDetachedFromWindow์— ๋Œ€ํ•ด ์ž˜ ๋ชจ๋ฅด๊ณ  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค๋Š” ๊ฒƒ์ด ์ด๋ ‡๊ฒŒ ๋“œ๋Ÿฌ๋‚ฌ์Šต๋‹ˆ๋‹ค. ๋ถ€๋„๋Ÿฝ์Šต๋‹ˆ๋‹ค.๐Ÿ˜”๐Ÿ˜ข

 

์ด๋Ÿฐ ์ƒํ™ฉ์„ ๋งž์ดํ•˜๊ณ  ๋‚˜์„œ์•ผ 'RecyclerView์— RxJava๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ๋งž๊ธฐ๋Š” ํ•œ๊ฑด๊ฐ€?'๋ผ๋Š” ์งˆ๋ฌธ์ด ๋“ค์—ˆ์–ด์š”. Activity๋‚˜ Fragment๊ฐ€ destroyed ๋˜๋Š” ์ƒํƒœ์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์—์„œ dispose()๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์ฝ”๋“œ๋“ค๋งŒ ๋ดค์ง€, RecyclerView์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์‚ฌ์‹ค ๋ณด์ง€ ๋ชปํ–ˆ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค๋”๊ตฐ์š”.

 

๊ทธ๋ฆฌ๊ณ  ๋„ˆ๋ฌด๋‚˜ ๊ฐ์‚ฌํ•œ ๊ธ€์„ ์ฐพ์•˜์–ด์š”.

 

https://stackoverflow.com/questions/57609883/where-to-clear-compositedisposable-in-recyclerview-adapter

 

Where to clear CompositeDisposable in RecyclerView.Adapter

Consider we have some Disposable in our RecyclerView.Adapter and we added them to a CompositeDisposable. which Adapter method callback is the best choice to clear() the CompositeDisposable ? Curr...

stackoverflow.com

์ œ๊ฐ€ ๋„์›€์„ ๋ฐ›์€ ๋ถ€๋ถ„์€ ์—ฌ๊ธฐ์ž…๋‹ˆ๋‹ค.

 

It would be easier to answer if you could provide the code of the adapter. In general, Disposable should be disposed with respect to your business logic and the component containing lifecycle.

I would also say that it is better to not use Rx inside of RecyclerView adapter. Here are the benefits:
- Adapter logic remains simple and synchronous.
- You don't need to think about possible lifecycle or multithreading issues when developing the adapter.
- Rx streams always allocate a lot of memory, so (if we talk about RecyclerView) putting them into the wrong place can lead to performance issues.

So, I'd suggest moving Rx streams into Presenter/ViewModel or similar component.

 

๋ฐœ๋ฒˆ์—ญ์„ ํ•˜์ž๋ฉด

๋‹น์‹ ์ด ์–ด๋Œ‘ํ„ฐ์— ์ฝ”๋“œ๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒŒ ๋” ์‰ฌ์šธ ๊ฑฐ์—์š”. ๋ณดํ†ต, Disposable์€ ๋‹น์‹ ์˜ ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง๊ณผ ์ปดํฌ๋„ŒํŠธ์˜ ๋ผ์ดํ”„์‚ฌ์ดํด์„ ๋”ฐ๋ผ dispose๋˜์–ด์•ผ๋งŒํ•ด์š”.

๋˜ RecyclerView ์–ด๋Œ‘ํ„ฐ ์•ˆ์—๋Š” Rx๋ฅผ ์“ฐ์ง€ ์•Š๋Š”๊ฒŒ ๋‚ซ๋‹ค๋Š” ๋ง์„ ํ•˜๊ณ  ์‹ถ๋„ค์š”.

-์–ด๋Œ‘ํ„ฐ ๋กœ์ง์€ ๊ฐ„๋‹จํ•˜๊ณ  ๋™๊ธฐ์ ์ด์ฃ 
-์–ด๋Œ‘ํ„ฐ๋ฅผ ๊ฐœ๋ฐœํ•  ๋•Œ์— ๊ฐ€๋Šฅํ•œ ๋ผ์ดํ”„์‚ฌ์ดํด ๋˜๋Š” ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ ์ด์Šˆ๋“ค์€ ์ƒ๊ฐํ•  ํ•„์š”๊ฐ€ ์—†์–ด์š”.
-Rx ์ŠคํŠธ๋ฆผ์€ ํ•ญ์ƒ ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•ด์„œ, ๋งŒ์•ฝ (์šฐ๋ฆฌ๊ฐ€ ๋ฆฌ์‹ธ์ดํด๋Ÿฌ๋ทฐ์— ๋Œ€ํ•ด์„œ ๋งํ•œ๋‹ค๋ฉด) ์ž˜๋ชป๋œ ๊ณณ์— ๊ฐ€์ ธ๋‹ค ์“ฐ๋ฉด ํผํฌ๋จผ์Šค ์ด์Šˆ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์–ด์š”.

๊ทธ๋ž˜์„œ Rx ์ŠคํŠธ๋ฆผ์„ Presenter/ViewModel ๋˜๋Š” ๋น„์Šทํ•œ ์ปดํฌ๋„ŒํŠธ๋กœ ์˜ฎ๊ธฐ๋Š” ๊ฒƒ์„ ๊ถŒํ•ด์š”.

์ €๋ ‡๊ฒŒ ์นœ์ ˆํ•˜๊ฒŒ ๊ธ€์„ ์ž‘์„ฑํ•ด์ฃผ์‹  Dimsky๋‹˜๊ป˜ ๊ฐ์‚ฌ๋ฅผ ๋“œ๋ฆฌ๋ฉด์„œ, ์ฝ”๋“œ ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

ํ•ด๊ฒฐ๋˜์„œ ๋‹คํ–‰์ด์—์š”. ๋๐Ÿ˜€