Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read only CurrentValueSubject?

Tags:

swift

combine

Is there a way to create a CurrentValueSubject that is read-only?

So you could sink it publicly, read value publicly, but could only send values to it internally/privately. Want to use it in a library module.

like image 688
Geri Borbás Avatar asked Oct 13 '25 03:10

Geri Borbás


1 Answers

The best pattern is to have it declared private:

private let _status = CurrentValueSubject<ThisStatus?, Never>(nil)

and expose it through a computed property:

public var status: AnyPublisher<ThisStatus?, Never> {
    _status
        .eraseToAnyPublisher()
}
like image 197
LuLuGaGa Avatar answered Oct 15 '25 13:10

LuLuGaGa