idghst.dev
article thumbnail
Published 2023. 3. 6. 09:00
[ Swift ] Combine - Subscriber Swift
728x90
반응형

1. .sink

  • 기본 구독 방식입니다
  • Publisher에서 전달받은 값에 대하여
    코드를 실행합니다
  • 기본적으로 전달받은 값에 대한 코드를 실행하지만
    실행 전, subscription에 대한 성공 / 실패 여부에 따른 코드도 추가로 실행 가능합니다.
<swift />
import Foundation import Combine let arrayPublisher = [1, 2, 3, 4, 5].publisher let arraySubscriber = arrayPublisher.sink { value in print("recived value : \(value)") } /* recived value : 1 recived value : 2 recived value : 3 recived value : 4 recived value : 5 */

 

2. .assign

  • sink와 같이 구독 관련 함수입니다
  • sink가 받은 값을 사용하여 코드를 직접 실행하는 것과 달리
    assign은 받은 값을 다른 객체의 property에 적용하는 함수입니다
  • 실행은 해당 객체(class)에서 didSet 함수를 이용하여
    property 변경에 대한 코드를 작성 해주면 됩니다
<swift />
import Foundation import Combine let arrayPublisher2 = [1, 2, 3, 4, 5].publisher class MyClass { var property: Int = 0 { didSet { print("Did set property : \(property)") } } } let Object = MyClass() let arraySubscriber2 = arrayPublisher2.assign(to: \.property, on: Object) print("Final Value : \(Object.property)") /* Did set property : 1 Did set property : 2 Did set property : 3 Did set property : 4 Did set property : 5 Final Value : 5 */

 

3. .subscribe

  • 구독 하는 함수 라고 생각하면 됩니다.
  • [publisher].subscribe([subscriber]) 처럼 사용하는데
    [subscriber][publisher] 를 구독한다 라고 해석됩니다
  • 아래 코드는 쉽게 생각하면
  • subscription1passthroughSubject1를 구독하고
    passthroughSubject1publisher를 구독합니다
  • passthroughSubject1publisher를 구독하기 전에는
    passthroughSubject1 만 데이터를 제공할 수 있었고
  • passthroughSubject1publisher를 구독한 후에는
    publisher가 제공한 데이터를 passthroughSubject1가 받아서 subscription1에게 전달하게 됩니다
  • 그런데, .publisher 의 특성상 제공할 데이터를 가지고 있으며
    모두 제공한 후에는 ‘완료’ 를 의미하는 .finish 를 마지막에 추가로 제공하기 때문에
    .finish 를 제공받은 passthroughSubject1는 그 데이터를 subscription1에 그대로 제공하기 때문에
    아래 코드의 모든 구독 관계가 끊어지게 됩니다
<swift />
import Foundation import Combine let passthroughSubject1 = PassthroughSubject<String, Never>() let subscription1 = passthroughSubject1 .print("debug") .sink { value in print("subscription1 : \(value)"); } passthroughSubject1.send("sample1") let publisher = ["1","2","3","4","5"].publisher publisher.subscribe(passthroughSubject1) passthroughSubject1.send("sample2") /* debug: receive subscription: (PassthroughSubject) debug: request unlimited debug: receive value: (sample1) subscription1 : sample1 debug: receive value: (1) subscription1 : 1 debug: receive value: (2) subscription1 : 2 debug: receive value: (3) subscription1 : 3 debug: receive value: (4) subscription1 : 4 debug: receive value: (5) subscription1 : 5 debug: receive finished */

 

4. .cancel()

  • 구독 취소를 의미합니다
  • Publisher가 값을 전달하더라도 더 이상 받지 않습니다
  • Publisher.send(completion: .finished) 와 동일한 기능입니다
<swift />
import Foundation import Combine let passthroughSubject2 = PassthroughSubject<String, Never>() let subscription2 = passthroughSubject2 .print("debug") .sink { value in print("subscription1 : \(value)"); } passthroughSubject2.send("hello world 1") passthroughSubject2.send("hello world 2") passthroughSubject2.send("hello world 3") subscription2.cancel() passthroughSubject2.send("hello world 4") /* debug: receive subscription: (PassthroughSubject) debug: request unlimited debug: receive value: (hello world 1) subscription1 : hello world 1 debug: receive value: (hello world 2) subscription1 : hello world 2 debug: receive value: (hello world 3) subscription1 : hello world 3 debug: receive cancel */
728x90
반응형

'Swift' 카테고리의 다른 글

[ Swift ] Combine - 실습  (0) 2023.03.13
[ Swift ] Combine - Operator  (0) 2023.03.08
[ Swift ] Combine - Publisher  (0) 2023.03.03
[ Swift ] Combine - 개념  (0) 2023.03.01
[ Swift ] 랜덤 설정 관련  (0) 2023.02.15
profile

idghst.dev

@idghst.dev

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!