Publishers.LastWhere
์ ๋ค๋ฆญ ๊ตฌ์กฐ์ฒด | ์คํธ๋ฆผ์ด ์ข ๋ฃํ๋ฉด, ํน์ ์กฐ๊ฑด ํด๋ก์ ๋ฅผ ๋ง์กฑํ๊ธฐ ์ํ ์คํธ๋ฆผ์ ๋ง์ง๋ง ์์๋ง์ ๋ฐํํ๋ Publisher
์ด๋์ ๋ผ์ด์ ๋ ๋ ๊ฐ์ ์ธ์๋ฅผ ๋ฐ๋๋ค.
upstream
: ์์์ ํ๋ฅด๋ Publisherpredicate
: ์ด๋ค ์์๋ฅผ ๋ฐํํ ์ง ๊ฒฐ์ ํ๋ ํด๋ก์
์์ Publisher๋ก๋ถํฐ ์กฐ๊ฑด ํด๋ก์ ์ ๋ง์กฑํ๋ ์์๋ฅผ ๋จผ์ ๋ฝ์๋ด๊ณ , ๊ทธ ์ค์์ ๋ง์ง๋ง ์์๋ง์ ๋ฐํํ๋ค.
last
์คํผ๋ ์ดํฐ์ ๊ด๋ จ์ด ์๋ค.
// Publishers.LastWhere Publisher
Publishers
.LastWhere(upstream: Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3, 4, 5])) { !$0.isMultiple(of: 2) }
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine FirstWhere Error")
case .finished:
print("Combine FirstWhere Finish")
}
}, receiveValue: { value in
print("Combine FirstWhere : \(value)")
})
.store(in: &cancellables)
// last Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3, 4, 5])
.last { !$0.isMultiple(of: 2) }
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine FirstWhere Error")
case .finished:
print("Combine FirstWhere Finish")
}
}, receiveValue: { value in
print("Combine FirstWhere : \(value)")
})
.store(in: &cancellables)
// Combine LastWhere : 5
// Combine LastWhere Finish
์์ Publisher๋ 1, 2, 3, 4, 5์ ๊ฐ์ ์ฐจ๋ก๋๋ก ๋ด๊ณ , 2์ ๋ฐฐ์๊ฐ ์๋ ์กฐ๊ฑด์ ์ค์ ํ๋ค.
๊ฐ๋ค ์ค 2์ ๋ฐฐ์๊ฐ ์๋ 1, 3, 5 ์ค ๋ง์ง๋ง ์์๋ 5์ด๋ฏ๋ก, ์ต์ข ์ ์ผ๋ก 5์ ๊ฐ์ ๋ด๊ณ ์ข ๋ฃํ๋ค.
RxSwift
ํด๋น ๋์์ ๊ตฌํํ๊ธฐ ์ํ ์คํผ๋ ์ดํฐ๋ฅผ ์ ๊ณตํ์ง ์๋๋ค.
ReactiveSwift
ํด๋น ๋์์ ๊ตฌํํ๊ธฐ ์ํ ์คํผ๋ ์ดํฐ๋ฅผ ์ ๊ณตํ์ง ์๋๋ค.
Last updated
Was this helpful?