μ λ€λ¦ ꡬ쑰체 | μ΄μ μμμ μΌμΉνμ§ μλ μμλ§ λ°ννλ Publisher
μ΄λμ
λΌμ΄μ λ λ κ°μ μΈμλ₯Ό λ°λλ€.
upstream
: μμμ νλ₯΄λ Publisher
predicate
: λ μμκ° μ€λ³΅λμλμ§ νκ°νκΈ° μν ν΄λ‘μ .
λ°νλλ €λ μμκ° μ΄μ μ λ°νλ κ²κ³Ό μΌμΉνλ€λ©΄ ν΄λΉ μμλ₯Ό λ°ννμ§ μλλ€.
μ€λ³΅λ κ°μ΄ λ€μ΄μ€λ κ²½μ° κ°μ κ°μ λ°λ³΅ λ°ννλ κ²μ λ°©μ§νκΈ° μν΄ μ¬μ©νλ€.
removeDuplicates
μ€νΌλ μ΄ν°μ κ΄λ ¨μ΄ μλ€.
ν΄λΉ μ€νΌλ μ΄ν°λ₯Ό μ¬μ©ν λ Output νμ
μ΄ Equatable
νλ‘ν μ½μ ꡬννκ³ μλ€λ©΄ predicate
ν΄λ‘μ λ₯Ό ꡬνν νμκ° μλ€.
// Publishers.RemoveDuplicates Publisher
Publishers.RemoveDuplicates(upstream: Publishers.Sequence<[Int], Never>(sequence: [1, 2, 2, 2])) { $0 == $1 }
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine RemoveDuplicates Error")
case .finished:
print("Combine RemoveDuplicates Finish")
}
}, receiveValue: { value in
print("Combine RemoveDuplicates : \(value)")
})
.store(in: &cancellables)
// removeDuplicates Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 2, 2, 2])
.removeDuplicates()
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine RemoveDuplicates Error")
case .finished:
print("Combine RemoveDuplicates Finish")
}
}, receiveValue: { value in
print("Combine RemoveDuplicates : \(value)")
})
.store(in: &cancellables)
// Combine RemoveDuplicates : 1
// Combine RemoveDuplicates : 2
// Combine RemoveDuplicates Finish
μμμ νλ₯΄λ Publisherκ° 1, 2, 2, 2μ κ°μ μ°¨λ‘λλ‘ λ΄λλ°, 2κ° μ€λ³΅λμ΄ λ΄μ΄μ§ λ 무μλλ€. κ·Έλ¬λ―λ‘ 1, 2μ κ°μ μ°¨λ‘λλ‘ λ΄κ³ μ’
λ£νκ² λλ€.
RxSwift
distinctUntilChanged
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
Observable.from([1, 2, 2, 2])
.distinctUntilChanged()
.subscribe(onNext: { value in
print("RxSwift RemoveDuplicates : \(value)")
}, onError: { _ in
print("RxSwift RemoveDuplicates Error")
}, onCompleted: {
print("RxSwift RemoveDuplicates Finish")
})
.disposed(by: disposeBag)
// RxSwift RemoveDuplicates : 1
// RxSwift RemoveDuplicates : 2
// RxSwift RemoveDuplicates Finish
ReactiveSwift
skipRepeats
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
SignalProducer([1, 2, 2, 2])
.skipRepeats()
.start { event in
switch event {
case let .value(value):
print("ReactiveSwift RemoveDuplicates : \(value)")
case .failed:
print("ReactiveSwift RemoveDuplicates Error")
case .completed:
print("ReactiveSwift RemoveDuplicates Finish")
default:
break
}
}
// ReactiveSwift RemoveDuplicates : 1
// ReactiveSwift RemoveDuplicates : 2
// ReactiveSwift RemoveDuplicates Finish
μ°Έκ³
ReactiveX - Operators - Distinct