Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing one property in a variable triggers subcriptions to other properties. RxSwift

I have the following struct of properties for Chat

struct Chat {
    var id = String()
    var gender = String()
    var date = Date()

    init() {}
}

In a view controller, i declare an instance of Chat called observablechat, and then i used the flatmap operator to attempt and observe only changes in the date property of observablechat. However, if i change the gender property (as shown), the subscription gets triggered. I wonder why that is and how can i fix this code such that the subscription only looks at what happens to the date property and nothing else?

class ViewController: UIViewController {

    var observablechat = Variable(Chat())

        observablechat.asObservable()
        .flatMap { (Chat) -> Observable<Date> in
            return Observable.of(Chat.matchDate)
        }.subscribe(onNext: { (r) in
            print(r)
        }).addDisposableTo(disposeBag)


        self.observablechat.value.gender = "male" 
        //triggers subscription above. 
    }
}
like image 727
Ryan Avatar asked Apr 29 '17 14:04

Ryan


1 Answers

First of all, why flatMap?

You need only distinctUntilChanged and map. DebugPrint will be triggered only twice: initial setup + date changed once. Check out the code below and feel free to ask questions.

import PlaygroundSupport
import RxSwift

struct Chat {

    var id: String
    var gender: String
    var date: Date

    init(id: String = "", gender: String = "", date: Date = Date(timeIntervalSince1970: 0)) {
        self.id = id
        self.gender = gender
        self.date = date
    }
}

final class YourClass {

    lazy var variableChat: Variable<Chat> = Variable<Chat>(Chat())

    var observableDate: Observable<Date> {
        return variableChat
            .asObservable()
            .distinctUntilChanged({ (chatOld, chatNew) -> Bool in
                return chatOld.date == chatNew.date
            })
            .map({ (chat) -> Date in
                return chat.date
            })
            .shareReplay(1)
    }
}

let value = YourClass()
value.observableDate
    .subscribe(onNext: { (date) in
        debugPrint(date.timeIntervalSince1970)
    })
value.variableChat.value.gender = "male"
value.variableChat.value.date = Date()
value.variableChat.value.gender = "female"
value.variableChat.value.gender = "male"
value.variableChat.value.gender = "female"

P.S. the way to run RxSwift in playground: readme

like image 178
iWheelBuy Avatar answered Oct 19 '22 01:10

iWheelBuy