See below code.
class ViewController4: UIViewController { var disposeBag = DisposeBag() let v = Variable(0) override func viewDidLoad() { super.viewDidLoad() v.asObservable() .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) v.value = 1 } }
When it runs, it will print
0 1
However, I don't want it to run on 0
, or saying 0
is just the value used to initiate v
. Can I do that? Or I have to postpone the code at the time point when I use it?
You can use operator .skip
to suppress N first element emited. So in your case skip(1)
will supress the init value.
http://reactivex.io/documentation/operators/skip
v.asObservable().skip(1) .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) v.value = 1 //Output : 1 v.value = 2 v.value = 3 //Output : 1 2 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With