본문 바로가기

IOS🍎/Combine5

[Combine] Cancellable이란 element들을 방출하는 스트림을 다 사용했다면 cancel() 해줘야 자원이 dealloc 될 수 있는데요. 즉 이름 그대로 cancellation(취소)를 위한 프로토콜입니다. 만약 cancel()을 호출하지 않으면 어떻게 될까요 ? viewController의 deinit()이 호출되고 나도 스트림이 살아있기 때문에 값을 방출하게 됩니다. deinit { print("ViewController deinit") } let publisher = [1, 2, 3].publisher publisher .map { $0 * 2 } .sink { value in print("\(value)") } //ViewController deinit //2 //4 //6 스트림이 살아있다면 Timer, network.. 2023. 4. 17.
[Combine] Scheduler란 무려 1년만의 combine 글 작성.... 이내요....ㅎ 오늘은 이러쿵 저러쿵 공부를 하던 와중 combine이 머였더라 .. ㅎ 하고 복습을 하게 되었고 Scheduler 정리를 드디어 하려구 합니다 Scheduler closure의 실행 시기와 방법을 정의하는 프로토콜 그렇습니다. scheduler도 프로토콜이네요 ! protocol Scheduler 공식문서 Overview를 보면 You can use a scheduler to execute code as soon as possible, or after a future date. Individual scheduler implementations use whatever time-keeping system makes sense for them. S.. 2023. 4. 16.
[Combine] ConnectablePublisher ConnectablePublisher connecting, canceling의 명시적인 의미를 가진 publisher subject와 같이 publisher의 일부입니다. element를 생산하기 전 추가적인 configuration, setup을 수행할 필요가 있을때, ConnectablePublisher를 사용합니다. connect() 메소드 호출 전, publisher는 어떤 element도 생산하지 않습니다. makeConnectable()을 사용하여 ConnectablePublisher을 생성합니다. private func connectablePublisher(){ let publisher = ["hi", "hello"].publisher.makeConnectable() let cancellab.. 2022. 5. 3.
[Combine] Subject란 (AnyPublisher) 정의 : publisher, subscriber와 같이 프로토콜이네요 ! 그리고 subject는 사실 publisher랍니다. subject는 Publisher 프로토콜을 채택하고 있습니다. 그리고 send() 메소드를 통해 값을 주입합니다. 즉 publisher는 값을 가지고 있는 주체였다면 이 값에 다른 값을 주입할 수 있는 것이 바로 subject입니다. subject는 다음과 같이 두가지 클래스로 구현될 수 있습니다. PassthroughSubject CurrentValueSubject PassthroughSubject downstream 구독자들에게 요소를 방출하는 broadcast 합니다. 예시를 봅시다. PassthroughSubject를 만들어 subscriber1과 subscriber2를.. 2022. 4. 17.
[Combine] Combine, Publisher, Subscriber 에 대해서 오늘은 Apple 에서 만든 Reactive 프로그래밍을 위한 프레임 워크 "Combine"에 대해 정리 해보려고 합니다. 그리곤 WWDC2021에서 새로 나온 Concurrency에 대해서도 알아볼 예정입니다. ➡️ 오늘의 목표 : Combine, Publisher, Subscriber, Publisher ↔️ Subscriber 정의 : 이벤트 처리 연산자들을 통해 비동기 이벤트들을 핸들링 할 수 있게 하는 프레임워크 Combine 프레임워크는 시간이 흐름에 따른 값 처리를 위한 선언적 Swift API를 제공합니다. 이러한 값은 다양한 종류의 비동기 이벤트를 나타낼 수 있습니다. Combine은 시간이 지남에 따라 변경될 수 있는 값을 노출하는 Publisher와 Publisher로부터 해당 값을 .. 2022. 4. 17.