# Subscribing for the changes in the environment init body

**URL:** <https://forums.swift.org/t/subscribing-for-the-changes-in-the-environment-init-body/41724>\
**Category:** Swift Composable Architecture\
**Created:** [November 3, 2020, 6:53am UTC](https://forums.swift.org/t/subscribing-for-the-changes-in-the-environment-init-body/41724 "2020-11-03T06:53:48Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![mbrandonw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mbrandonw/32/349_2.png) [@mbrandonw](https://forums.swift.org/u/mbrandonw)\
**Post date:** [November 4, 2020, 1:39pm UTC](https://forums.swift.org/t/subscribing-for-the-changes-in-the-environment-init-body/41724/3 "2020-11-04T13:39:50Z")

</div>

What you can do is put the publisher in your environment and then use something like a `didFinishLaunching` action to subscribing to it:

```swift
struct HomeState { ... }
enum HomeAction {
  case didFinishLaunching
  case dataResponse(Result<SomeData, SomeError>)
  // other actions
}
struct HomeEnvironment {
  var data: Effect<SomeData, SomeError>
  // other dependencies
}

let homeReducer = Reducer<HomeState, HomeAction, HomeEnvironment> { state, action, environment in 
  switch action { 
  case .didFinishLaunching:
    return environment.data
      .catchToEffect()
      .map(HomeAction.dataResponse)

  case let .dataResponse(.success(data)):
    // do something with data

  case let .dataResponse(.failure(error)):
    // do something with error
  }
}

```

Hope it helps.

---

_[View the full topic](https://forums.swift.org/t/subscribing-for-the-changes-in-the-environment-init-body/41724)._
