24

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?

Owen Zhao
  • 3,205
  • 1
  • 26
  • 43

1 Answers1

51

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
Makaille
  • 1,636
  • 2
  • 24
  • 41