μ λ€λ¦ ꡬ쑰체 | λ€λ₯Έ 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
μ€νΌλ μ΄ν°μ κΈ°λ₯μ ꡬνν μ μλ€.
// concat Operator
Observable.just(1)
.concat(Observable.just(2))
.subscribe(onNext: { value in
print("RxSwift Concatenate : \(value)")
}, onError: { _ in
print("RxSwift Concatenate Error")
}, onCompleted: {
print("RxSwift Concatenate Finish")
})
.disposed(by: disposeBag)
// RxSwift Concatenate : 1
// RxSwift Concatenate : 2
// RxSwift Concatenate Finish
// startWith Operator
Observable.just(1)
.startWith(2)
.subscribe(onNext: { value in
print("RxSwift Concatenate : \(value)")
}, onError: { _ in
print("RxSwift Concatenate Error")
}, onCompleted: {
print("RxSwift Concatenate Finish")
})
.disposed(by: disposeBag)
// RxSwift Concatenate : 2
// RxSwift Concatenate : 1
// RxSwift Concatenate Finish
ReactiveSwift
concat
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ append
μ€νΌλ μ΄ν°μ κΈ°λ₯μ ꡬνν μ μλ€.
prefix
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ prepend
μ€νΌλ μ΄ν°μ κΈ°λ₯μ ꡬνν μ μλ€.
// concat Operator
SignalProducer(value: 1)
.concat(value: 2)
.start { event in
switch event {
case let .value(value):
print("ReactiveSwift Concatenate : \(value)")
case .failed:
print("ReactiveSwift Concatenate Error")
case .completed:
print("ReactiveSwift Concatenate Finish")
default:
break
}
}
// ReactiveSwift Concatenate : 1
// ReactiveSwift Concatenate : 2
// ReactiveSwift Concatenate Finish
// prefix Operator
SignalProducer(value: 1)
.prefix(value: 2)
.start { event in
switch event {
case let .value(value):
print("ReactiveSwift Concatenate : \(value)")
case .failed:
print("ReactiveSwift Concatenate Error")
case .completed:
print("ReactiveSwift Concatenate Finish")
default:
break
}
}
// ReactiveSwift Concatenate : 2
// ReactiveSwift Concatenate : 1
// ReactiveSwift Concatenate Finish
μ°Έκ³
ReactiveX - Operators - StartWith