Skip to content

Sync AppState with children on lower levels #1121

Discussion options

You must be logged in to vote

If I quote @mbrandonw correctly you should handle global state inside the environment.
You could do this by creating a Service that provides a publisher. This publisher then can be erased to an Effect, and observed inside the reducer to update the state accordingly.

struct PublisherViewEnvironment {
    let publisherEffect: () -> Effect<Int, GeneralError> = {
        return Effect(SomeService().publisher)
    }
}

class SomeService {
    var publisher: AnyPublisher<Int, GeneralError> {
        return subject.eraseToAnyPublisher()
    }
    private var subject = CurrentValueSubject<Int,GeneralError>(0)
    private var timer: Timer?
    
    init() {
        self.timer = Timer.scheduledTimer(

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by danielhorv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants