티스토리 뷰
728x90
반응형
viewController가 deinit 될때, 그 안의 Pulisher가 auto-cancel 되는지 실험해봅시다 --!
5초 후에 이벤트를 발행하는 Publisher와 3초 후에 deinit되는 viewController가 있습니다
1) stream을 안담아줄 때
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
deinit { | |
print("viewController deinit") | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let stream = Publishers.Just("is Alive?") | |
.delay(for: .seconds(5), scheduler: RunLoop.main) | |
.sink(receiveCompletion: { (completion) in | |
switch completion { | |
case .finished: print("finished") | |
case .failure(let error): print(error) | |
} | |
}, receiveValue: { | |
print($0) | |
}) | |
DispatchQueue.main.asyncAfter(deadline: .now() + 3) { | |
UIApplication.shared.keyWindow?.rootViewController = nil | |
} | |
} | |
<output> | |
viewController deinit | |
is Alive? | |
finished |
viewController가 deinit되어도 스트림이 살아있습니다
2) stream을 Cancellable에 담아줄 때
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
var cancellable: Cancellable? | |
deinit { | |
print("viewController deinit") | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let stream = Publishers.Just("is Alive?") | |
.delay(for: .seconds(5), scheduler: RunLoop.main) | |
.sink(receiveCompletion: { (completion) in | |
switch completion { | |
case .finished: print("finished") | |
case .failure(let error): print(error) | |
} | |
}, receiveValue: { | |
print($0) | |
}) | |
cancellable = stream | |
DispatchQueue.main.asyncAfter(deadline: .now() + 3) { | |
UIApplication.shared.keyWindow?.rootViewController = nil | |
} | |
} | |
<output> | |
viewController deinit | |
is Alive? | |
finished |
viewController가 deinit되어도 여전히 스트림이 살아있습니다
3) stream을 Cancellable에 담아주고 deinit 때 cancel 시켜줄 때
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
var cancellable: Cancellable? | |
deinit { | |
print("viewController deinit") | |
cancellable?.cancel() | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let stream = Publishers.Just("is Alive?") | |
.delay(for: .seconds(5), scheduler: RunLoop.main) | |
.sink(receiveCompletion: { (completion) in | |
switch completion { | |
case .finished: print("finished") | |
case .failure(let error): print(error) | |
} | |
}, receiveValue: { | |
print($0) | |
}) | |
cancellable = stream | |
DispatchQueue.main.asyncAfter(deadline: .now() + 3) { | |
UIApplication.shared.keyWindow?.rootViewController = nil | |
} | |
} | |
<output> | |
viewController deinit |
이렇게 deinit 될때 cancel시키는 코드를 넣어줘야지 스트림이 같이 끝납니다
4) stream을 AnyCancellable에 담아줄 때
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
var cancellable: AnyCancellable? | |
deinit { | |
print("viewController deinit") | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let stream = Publishers.Just("is Alive?") | |
.delay(for: .seconds(5), scheduler: RunLoop.main) | |
.sink(receiveCompletion: { (completion) in | |
switch completion { | |
case .finished: print("finished") | |
case .failure(let error): print(error) | |
} | |
}, receiveValue: { | |
print($0) | |
}) | |
cancellable = AnyCancellable(stream) | |
DispatchQueue.main.asyncAfter(deadline: .now() + 3) { | |
UIApplication.shared.keyWindow?.rootViewController = nil | |
} | |
} | |
<output> | |
viewController deinit |
AnyCancellable에 담아줘야지 deinit 될 때 auto-cancel 됩니다
AnyCancellable로 스트림의 메모리 관리를 해줘야합니다...!!
Cancellable과 AnyCancellable이 어떻게 다른 지는 RxSwift vs Combine - 스펙 / 성능 / 개념 비교 이 포스팅에 적어두었구요
왜 이런 차이가 나는 지는 저도 잘 모르겠습니다 😭 누가 좀 알려주세요오... 정말 궁금..
반응형
'🍏 > SwiftUI + Combine' 카테고리의 다른 글
[SwiftUI] 뷰의 이니셜라이저에서 @State, @Binding, @EnvironmentObject에 접근하기 (1) | 2020.08.31 |
---|---|
[SwiftUI] Multi-platform App 만들기 - 프로젝트 세팅 (3) | 2020.08.06 |
[SwiftUI] ZStack 실전 예제들 (2) | 2020.05.16 |
Rx에서 Combine으로 바꿔본 후기와 느낀점 (2) | 2019.06.28 |
RxSwift vs Combine - 스펙 / 성능 / 개념 비교 (16) | 2019.06.28 |
댓글
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- Flutter Spacer
- Watch App for iOS App vs Watch App
- METAL
- flutter build mode
- drf custom error
- Sketch 누끼
- DRF APIException
- flutter 앱 출시
- SerializerMethodField
- Django Heroku Scheduler
- cocoapod
- Flutter 로딩
- github actions
- Flutter getter setter
- 장고 Custom Management Command
- Flutter Text Gradient
- Flutter Clipboard
- flutter deep link
- flutter dynamic link
- 플러터 싱글톤
- Django Firebase Cloud Messaging
- PencilKit
- 구글 Geocoding API
- 플러터 얼럿
- 장고 URL querystring
- ribs
- Python Type Hint
- Django FCM
- ipad multitasking
- Dart Factory
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함