๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“Œ Language/Swift

[RxSwift] Disposable๊ณผ DisposeBag

by Fomagran ๐Ÿ’ป 2020. 12. 26.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค.

 

์˜ค๋Š˜์€ RxSwift์—์„œ ๊ฐ€์žฅ ๊ธฐ์ดˆ๊ฐ€๋˜๋Š” Disposable๊ณผ DisposeBag์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~

 

Disposable


๋จผ์ € dispose์˜ ์‚ฌ์ „์  ์˜๋ฏธ๋Š” ๋ญ˜๊นŒ์š”?

 

์—ฌ๋Ÿฌ๊ฐ€์ง€ ์˜๋ฏธ๊ฐ€ ์žˆ์ง€๋งŒ Rx์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์˜๋ฏธ๋Š” "์ฒ˜๋ฆฌํ•˜๋‹ค" ๋ผ๋Š” ๋œป์ด์—์š”. ("์ทจ์†Œํ•˜๋‹ค" ๋ผ๊ณ  ๋ณด์…”๋„ ๋ ๊ฑฐ๊ฐ™์•„์š”)

 

๊ทธ๋ ‡๋‹ค๋ฉด disposable์€ "์ฒ˜๋ฆฌํ• ์ˆ˜์žˆ๋Š”" ์ด๊ฒ ์ฃ ?

 

๋งŒ์•ฝ ์–ด๋–ค ์ž‘์—…์ด ์‹คํ–‰๋˜๋Š”๋ฐ ์ด๊ฒŒ ๋„ˆ๋ฌด ์˜ค๋ž˜๊ฑธ๋ฆฌ๊ฑฐ๋‚˜ ์•„๋‹ˆ๋ฉด ์ค‘๊ฐ„์— ์ทจ์†Œํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ์ค‘์ธ ์ž‘์—…์„

 

๋ฉˆ์ถฐ์•ผํ•˜๊ฒ ์ฃ ? 

 

๋งŒ์•ฝ Rx๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  GCD๋กœ ๋น„๋™๊ธฐ์ž‘์—…์„ ๊ตฌํ˜„ํ–ˆ๋‹ค๋ฉด ์ค‘๊ฐ„์— ์ทจ์†Œํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ณ 

 

๋งŒ์•ฝ ์ทจ์†Œํ•ด์•ผํ•œ๋‹ค๋ฉด OperationQueue๋ฅผ ์‚ฌ์šฉํ•ด์„œ cancelํ•ด์ค˜์•ผ ํ• ๊ฑฐ์—์š”.

 

Rx๋Š” cancel ๋Œ€์‹  disposable์„ ์‚ฌ์šฉํ•ด์„œ ์ž‘์—…์„ ์ทจ์†Œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์˜ˆ์ œ์™€ ํ•จ๊ป˜ ๋ณด์‹ค๊ฒŒ์š”.

 

์˜ˆ์ œ๋ฅผ ์„ค๋ช…๋“œ๋ฆฌ๋ฉด 0๋ถ€ํ„ฐ 10000000 ์นด์šดํŠธํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด 1๋ถ€ํ„ฐ 10000000๊นŒ์ง€ ์นด์šดํŠธ๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด ํƒ€์ด๋จธ๊ฐ€ ๋ฉˆ์ถ”๊ณ  Completed๋ฅผ ๋„์›Œ์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์— stop๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋น„๋™๊ธฐ์ž‘์—…์„ ๋ฉˆ์ถ”๊ฒŒ ํ•˜๋Š” ๊ฒƒ์„ ๊ตฌํ˜„ํ•˜๊ณ  ์‹ถ์œผ๋ฉด dispose๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์šฐ์„ ์€ disposable ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์‹œ๊ณ 

 

์œ„์˜ isCompletedCount ํ•จ์ˆ˜์— ๋ณ€์ˆ˜๋ฅผ disposable๋กœ ์ •ํ•ด์ฃผ์‹œ๊ณ 

stop์•ก์…˜์— ์ •ํ•ด์ค€ disposable์„ disposeํ•ด์ฃผ์‹œ๋ฉด ๋ฉˆ์ถ”๋Š” ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์ด stop๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋น„๋™๊ธฐ ์ž‘์—…์ด ๋ฉˆ์ถ”๊ฒŒ ๋˜๊ณ  ์ทจ์†Œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค๋กœ ๋ ˆ์ด๋ธ”์„ ๋„์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

DisposeBag


๋งŒ์•ฝ 0๋ถ€ํ„ฐ 10000๊นŒ์ง€ ์„ธ๋Š” ์ž‘์—…๊ณผ 0๋ถ€ํ„ฐ 20000๊นŒ์ง€ ์„ธ๋Š” ์ž‘์—… ๋“ฑ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋น„๋™๊ธฐ ์ž‘์—…์ด ์žˆ๊ณ  

 

์ด ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋น„๋™๊ธฐ ์ž‘์—…์„ ํ•œ๋ฒˆ์— ์ทจ์†Œํ•˜๋ ค๊ณ  ํ• ๋•Œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ์š”?

 

๋ฐ”๋กœ ์ด๋•Œ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋Š” ๊ฐ€๋ฐฉ์„ ๋งŒ๋“ค์–ด๋†“๊ณ  ์ด ๊ฐ€๋ฐฉ์„ ํ•œ๋ฒˆ์— ์ง€์›Œ๋ฒ„๋ฆฌ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์ด๋•Œ ์ฒ˜๋ฆฌ๊ฐ€ ๋ฐ”๋กœ disposable ์ด๊ณ  ๋‹ด๋Š” ๊ฐ€๋ฐฉ์ด DisposeBag ์ด์ฃ .

 

์‚ฌ์šฉ๋ฐฉ๋ฒ•์€ disposeBag์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณค ์˜ต์ €๋ฒ„๋ธ”์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋์— dispose๋ฅผ ๋‹ฌ์•„์ฃผ๊ณ  disposeBag์— ๋‹ด์•„์ค๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ๋‹ด๊ธด disposeBag์„ ๋น„์›Œ์ฃผ๋ฉด dispose๊ฐ€ ๋‹ฌ๋ฆฐ ํ•จ์ˆ˜๋“ค์€ ๋ชจ๋‘ ์ทจ์†Œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

๋ฐ”๋กœ ์•„๋ž˜์ฒ˜๋Ÿผ ํ•œ๋ฒˆ์— ๋‘ ๋น„๋™๊ธฐ ์ž‘์—…์ด ์ทจ์†Œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

Source Code


 

 

fomagran/RxSwift-Example

Contribute to fomagran/RxSwift-Example development by creating an account on GitHub.

github.com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import UIKit
import RxSwift
 
class ViewController: UIViewController {
    
    @IBOutlet weak var completed: UILabel!
    @IBOutlet weak var number: UILabel!
    
    var count = 0.0
    
    var timer:Timer?
    var disposable:Disposable?
 
    override func viewDidLoad() {
        super.viewDidLoad()
        startTimer()
        isCompletedCount()
    }
    
    func startTimer() {
        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(update), userInfo: nil, repeats: true)
    }
    
    @objc func update() {
        count += 0.1
        number.text = String(format: "%.1f",  count) + String("์ดˆ๊ฐ€ ๊ฑธ๋ ธ์–ด์š”")
    }
    
    @IBAction func stop(_ sender: Any) {
        disposable?.dispose()
        timer?.invalidate()
        completed.text = "์ทจ์†Œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
    }
    
    func isCompletedCount() {
        disposable = rxCountNumber()
            .observeOn(MainScheduler.instance)
            .subscribe({[weak self] _ in
                self?.completed.text = "Completed!"
            })
    }
    
    func countNumber(completed: @escaping (Bool?) -> Void) {
        DispatchQueue.global().async {
            for _ in 0...10000000 {
            
            }
            self.timer?.invalidate()
            completed(true)
        }
    }
    
    func rxCountNumber() -> Observable<Bool> {
        return Observable.create { [weak self] o in
            self?.countNumber { (result) in
                o.onNext(result ?? false)
                o.onCompleted()
            }
            return Disposables.create()
        }
    }
}
cs

 

Reference


์•„๋ž˜๋ฅผ ์ฐธ๊ณ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

iamchiwon/RxSwift_In_4_Hours

RxSwift, 4์‹œ๊ฐ„ ์•ˆ์— ๋น ๋ฅด๊ฒŒ ์ตํ˜€ ์‹ค๋ฌด์— ์‚ฌ์šฉํ•˜๊ธฐ. Contribute to iamchiwon/RxSwift_In_4_Hours development by creating an account on GitHub.

github.com

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€