简体   繁体   中英

How to do a serial execution on rxSwift variable?

Consider the following two variables:

let int = Variable<Int>(1)

let string = Variable<String>("hey!")

let's assume that they are all bound to some UI already, like:

int.asObservable.bind(to:variable1)
string.asObservable.bind(to:variable2)

How can I make sure that string.asObservable.bind(to:variable2) is only executed after int.asObservable.bind(to:variable1) has been executed?

Your int and string are two different variables . It means that each variable can be changed independently.

You can join them to be sure that these values are changed simultaneously and delay the 2nd binding this way:

let values = Variable<(int: Int, string: String)>((1, "hey!"))
values
    .asObservable()
    .map({ $0.int })
    .bind(to: variable1)
values
    .asObservable()
    .map({ $0.string })
    .delay(0.1, scheduler: MainScheduler.instance)
    .bind(to: variable2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM