μΌ | μ | ν | μ | λͺ© | κΈ | ν |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- RxSwift
- @Binding
- Bug
- IOS
- init?
- swift6
- NullObject
- SwiftUI
- Operater
- Operators
- ios14
- URL(string:)
- SWIFT
- vim
- @EnvironmentObject
- Creating Operators
- @Environment
- @State
- init
- dismiss
- graphql
- NavigationLink
- RxCocoa
- subject
- nonisolated
- nestjs
- operator
- Xcode
- RFC1738/1808
- typeorm
- Today
- Total
Tunko Development Diary
RxSwift) Creating Operators (repeatElement, deferred, create) λ³Έλ¬Έ
RxSwift) Creating Operators (repeatElement, deferred, create)
Tunko 2022. 5. 31. 16:02repeatElement
let disposeBag = DisposeBag()
let element = "π"
Observable.repeatElement(element)
.take(10)
.subscribe { print($0) }
.disposed(by: disposeBag)
μΆλ ₯
next(π)
next(π)
next(π)
next(π)
next(π)
next(π)
next(π)
next(π)
next(π)
next(π)
completed
λμΌν μμλ₯Ό λ°λ³΅μ μΌλ‘ λ°©μΆνλ μ°μ°μμ λλ€.
무νμ λ°λ³΅ν©λλ€.
λ°λΌμ λ°©μΆλλ νμλ₯Ό μ ν΄μ£Όλκ² μ€μν©λλ€. take μ°μ°μλ₯Ό μ¬μ©ν΄μ 10κ°μ μμλ§ λ°©μΆλλλ‘ ν΄μ£Όμμ΅λλ€.
deferred
let disposeBag = DisposeBag()
var flag = true
let observable : Observable<Int> = Observable.deferred {
flag.toggle()
if flag {
return Observable.just(1)
} else {
return Observable.just(2)
}
}
observable
.subscribe { print($0) }
.disposed(by: disposeBag)
μΆλ ₯
next(2)
completed
νΉμ 쑰건μ λ°λΌ μ΅μ λ²λΈμ μμ±νλ μ°μ°μ μ λλ€. flag 쑰건μ λ°λΌ 2κ° λ€μ΄κ° justμ΅μ λ²λΈμ΄ ꡬλ λκ²μ νμΈν μ μμ΅λλ€.
create
just, of, from, range, genarate, repeatElement, deferred λ±μ μ°μ°μλ μ΅μ λ²λΈμ λ°μ μμλ₯Ό λ°©μΆνκ³ completedλ₯Ό ν΅ν΄μ μλ£λ©λλ€
μ΄λ° λμ λ°©μμ λ°κΎΈκΈ° μν΄μ createλ₯Ό ν΅ν΄μ μλ‘μ΄ μ΅μ λ²λΈμ λ§λ€ νμκ° μμ΅λλ€.
createλ Observerμ νλΌλ―Έν°λ‘ λ°μμ Disposableμ 리ν΄ν©λλ€.
returnμ μ μ ν΄μΌ ν©λλ€. Disposableμ 리ν΄νλΌκ³ λμ΄μλλ° μ»΄νμΌ μλ¬κ° λ°μν©λλ€. κΌ Disposables.create() λ‘ λ¦¬ν΄ν΄μ€λλ€.
createμ΅μ λ²λΈμ ν΅ν΄ μμλ₯Ό λ°©μΆν λλ onNextλ©μλλ₯Ό μ¬μ©ν΄μΌ ν©λλ€.
createμ΅μ λ²λΈμ μ’ λ£νκΈ° μν΄μ onError, onCompleted μ€ νλλ₯Ό κΌ νΈμΆν΄μΌν©λλ€.
let disposeBag = DisposeBag()
enum MyError: Error {
case error
}
Observable<String>.create { (observer) -> Disposable in
observer.onNext("λ΄κ° νκ³ μΆμ μ²λ¦¬")
observer.onCompleted()
return Disposables.create()
}
.subscribe{ print($0) }
.disposed(by: disposeBag)
μΆλ ₯
next(λ΄κ° νκ³ μΆμ μ²λ¦¬)
completed
μΉμμ html λ¬Έμμ΄μ λ°λ μ΅μ λ²λΈ μμ±
Observable.create { (observer) -> Disposable in
guard let url = URL(string: "<https://huniroom.tistory.com/>") else {
observer.onError(MyError.error)
return Disposables.create()
}
guard let html = try? String(contentsOf: url, encoding: .utf8) else {
observer.onError(MyError.error)
return Disposables.create()
}
observer.onNext(html)
observer.onCompleted()
return Disposables.create()
}
.subscribe{ print($0) }
.disposed(by: disposeBag)
'Development > RxSwift' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
RxSwift) Filtering Operators (take) (0) | 2022.06.01 |
---|---|
RxSwift) Filtering Operators [ignoreElements, element(at:), filter, skip, skip(while:), skip(until:), skip(duration:scheduler:)] (0) | 2022.05.31 |
RxSwift) Relay (RxSwift 6) (0) | 2022.05.30 |
RxSwift) AsyncSubject λ? (0) | 2022.05.30 |
RxSwift) ReplaySubject λ? (0) | 2022.05.30 |