Prevent duplicate subscribers with RACSignal

100 Views Asked by At

Is there a way to prevent duplicate RACSignal subscribers?

Right now I've managed to fix this in a way I would like to see simplified.

I've got my signal:

ExampleAPI.ProgressList().signalValue.doCompleted({ () -> Void in
    self.titleProgressCallRunning = false
})

And when it's started, I only subscribe when titleProgressCallRunning is false:

private func getTitlesProgress() {
    if self.titleProgressCallRunning {
        return
    }

    self.titleProgressCallRunning = true

    titleProgressSignal.subscribeNextAs({ (titlesProgress:[VDLTitleProgress]) -> () in
        self.titlesProgress = titlesProgress

        NSNotificationCenter.defaultCenter().postNotificationName(kNotificationTitlesProgressUpdated, object: nil)
    })
}

I'm using Moya, so the request is only running once already. But the subscribeNextAs block is triggered twice without using the boolean titleProgressCallRunning if I call the getTitlesProgress method twice.

I'm looking for a nice solution to prevent this!

1

There are 1 best solutions below

0
On

Disclaimer: I have not seen the current state of RAC for Swift.

In the current version for objc, you simply use a RACMulticastConnection to share side effects between subscribers. More about it in the Design Guidelines.