Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to not trigger the init value of the Variable<T>?

Tags:

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?

like image 274
Owen Zhao Avatar asked Apr 19 '17 15:04

Owen Zhao


1 Answers

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 
like image 189
Makaille Avatar answered Oct 05 '22 03:10

Makaille