Publishers.Concatenate

์ œ๋„ค๋ฆญ ๊ตฌ์กฐ์ฒด | ๋‹ค๋ฅธ Publisher๊ฐ€ ์š”์†Œ๋ฅผ ๋ฐฉ์ถœํ•˜๊ธฐ ์ด์ „์— ํ•œ Publisher์˜ ์š”์†Œ๋ฅผ ๋ชจ๋‘ ๋ฐฉ์ถœํ•˜๋Š” Publisher

์ด๋‹ˆ์…œ๋ผ์ด์ €๋Š” ๋‘ ๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ›๋Š”๋‹ค.

  • prefix : suffix๊ฐ€ ์š”์†Œ๋ฅผ ๋‹ค์‹œ ๋ฐœํ–‰ํ•˜๊ธฐ ์ด์ „์— ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋‹ค์‹œ ๋ฐœํ–‰ํ•˜๋Š” Publisher

  • suffix : prefix Publisher๊ฐ€ ์ข…๋ฃŒํ•œ ์งํ›„ ๋‹ค์‹œ ๋ฐœํ–‰ํ•˜๋Š” Publisher

prefix Publisher์™€ suffix Publisher์˜ Output ํƒ€์ž…๊ณผ ์—๋Ÿฌ ํƒ€์ž…์€ ๋™์ผํ•ด์•ผ ํ•œ๋‹ค.

Publisher๋ฅผ ์ด์–ด ๋ถ™์ด๊ฑฐ๋‚˜ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

append ๋ฐ prepend ์˜คํผ๋ ˆ์ดํ„ฐ์™€ ๊ด€๋ จ์ด ์žˆ๋‹ค.

append ์˜คํผ๋ ˆ์ดํ„ฐ๋Š” ๋‹ค์Œ์˜ ํ˜•ํƒœ๋ฅผ ๊ฐ–๋Š”๋‹ค.

  • ๊ฐ€๋ณ€ ์ธ์ž๋ฅผ ๋ฐ›์•„ ๊ธฐ์กด Publisher๊ฐ€ ์ข…๋ฃŒํ•œ ํ›„ ์ด์–ด์„œ ๋ฐœํ–‰ํ•œ๋‹ค. ex) Just(1).append(2, 3)

  • ์‹œํ€€์Šค๋ฅผ ๋ฐ›์•„ ๊ธฐ์กด Publisher๊ฐ€ ์ข…๋ฃŒํ•œ ํ›„ ์ด์–ด์„œ ๋ฐœํ–‰ํ•œ๋‹ค. ex) Just(1).append([2, 3])

  • Publisher๋ฅผ ๋ฐ›์•„ ๊ธฐ์กด Publisher๊ฐ€ ์ข…๋ฃŒํ•œ ํ›„ ์ด์–ด์„œ ๋ฐœํ–‰ํ•œ๋‹ค. ex) Just(1).append(Just(2))

prepend ์˜คํผ๋ ˆ์ดํ„ฐ๋Š” ๋‹ค์Œ์˜ ํ˜•ํƒœ๋ฅผ ๊ฐ–๋Š”๋‹ค.

  • ๊ฐ€๋ณ€ ์ธ์ž๋ฅผ ๋ฐ›์•„ ๊ธฐ์กด Publisher์˜ ์•ž์— ์ด์–ด ๋ถ™์—ฌ ๋ฐœํ–‰ํ•œ๋‹ค. ex) Just(1).prepend(2, 3)

  • ์‹œํ€€์Šค๋ฅผ ๋ฐ›์•„ ๊ธฐ์กด Publisher์˜ ์•ž์— ์ด์–ด ๋ถ™์—ฌ ๋ฐœํ–‰ํ•œ๋‹ค. ex) Just(1).prepend([2, 3])

  • Publisher๋ฅผ ๋ฐ›์•„ ๊ธฐ์กด Publisher์˜ ์•ž์— ์ด์–ด ๋ถ™์—ฌ ๋ฐœํ–‰ํ•œ๋‹ค. ex) Just(1).prepend(Just(2))

// Publishers.Concatenate Publisher
Publishers
  .Concatenate(prefix: Just(1), suffix: Just(2))
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Concatenate Error")
    case .finished:
      print("Combine Concatenate Finish")
    }
  }, receiveValue: { value in
    print("Combine Concatenate : \(value)")
  })
  .store(in: &cancellables)

// append Operator
Just(1)
  .append(Just(2))
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Concatenate Error")
    case .finished:
      print("Combine Concatenate Finish")
    }
  }, receiveValue: { value in
    print("Combine Concatenate : \(value)")
  })
  .store(in: &cancellables)

// Combine Concatenate : 1
// Combine Concatenate : 2
// Combine Concatenate Finish

// prepend Operator
Just(1)
  .prepend(Just(2))
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Concatenate Error")
    case .finished:
      print("Combine Concatenate Finish")
    }
  }, receiveValue: { value in
    print("Combine Concatenate : \(value)")
  })
  .store(in: &cancellables)

// Combine Concatenate : 2
// Combine Concatenate : 1
// Combine Concatenate Finish

1๋ฒˆ ์ฝ”๋“œ์™€ 2๋ฒˆ ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ 1์˜ ๊ฐ’์„ ๋‚ด๋Š” Just Publisher์˜ ๋์— 2์˜ ๊ฐ’์„ ๋‚ด๋Š” Just Publisher๋ฅผ ์ด์–ด ๋ถ™์˜€๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ 1๊ณผ 2์˜ ๊ฐ’์„ ์ฐจ๋ก€๋Œ€๋กœ ๋‚ด๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.

3๋ฒˆ ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ 1์˜ ๊ฐ’์„ ๋‚ด๋Š” Just Publisher์˜ ์•ž์— 2์˜ ๊ฐ’์„ ๋‚ด๋Š” Just Publisher๋ฅผ ์ด์–ด ๋ถ™์˜€๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ 2์™€ 1์˜ ๊ฐ’์„ ์ฐจ๋ก€๋Œ€๋กœ ๋‚ด๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.

RxSwift

concat ์˜คํผ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ append ์˜คํผ๋ ˆ์ดํ„ฐ์˜ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

startWith ์˜คํผ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ prepend ์˜คํผ๋ ˆ์ดํ„ฐ์˜ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

ReactiveSwift

concat ์˜คํผ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ append ์˜คํผ๋ ˆ์ดํ„ฐ์˜ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

prefix ์˜คํผ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ prepend ์˜คํผ๋ ˆ์ดํ„ฐ์˜ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฐธ๊ณ 

ReactiveX - Operators - StartWith

Last updated

Was this helpful?