Publishers.Concatenate
์ ๋ค๋ฆญ ๊ตฌ์กฐ์ฒด | ๋ค๋ฅธ Publisher๊ฐ ์์๋ฅผ ๋ฐฉ์ถํ๊ธฐ ์ด์ ์ ํ Publisher์ ์์๋ฅผ ๋ชจ๋ ๋ฐฉ์ถํ๋ Publisher
์ด๋์ ๋ผ์ด์ ๋ ๋ ๊ฐ์ ์ธ์๋ฅผ ๋ฐ๋๋ค.
prefix:suffix๊ฐ ์์๋ฅผ ๋ค์ ๋ฐํํ๊ธฐ ์ด์ ์ ๋ชจ๋ ์์๋ฅผ ๋ค์ ๋ฐํํ๋ Publishersuffix:prefixPublisher๊ฐ ์ข ๋ฃํ ์งํ ๋ค์ ๋ฐํํ๋ 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 Finish1๋ฒ ์ฝ๋์ 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 ์คํผ๋ ์ดํฐ์ ๊ธฐ๋ฅ์ ๊ตฌํํ ์ ์๋ค.
์ฐธ๊ณ
Last updated
Was this helpful?