# multiplatform

Stefan Oltmann

06/29/2021, 2:56 PM
I get an error "EXC_BAD_ACCESS" when I try to call "onReceive":
Copy code
struct ContentView: View {
  @EnvironmentObject var store: ObservablePhotoStore
  var body: some View {
    VStack {
      Button("Show details") {
    onReceive(store.$state) { value in
This is the observerved object:
Copy code
class ObservablePhotoStore: ObservableObject {
  @Published public var state: PhotoState = PhotoState(progress: false, photos: [], selectedPhoto: nil, testString: "never updated")
  @Published public var sideEffect: PhotoSideEffect?
  let store: PhotoStore
  var stateWatcher : Closeable?
  var sideEffectWatcher : Closeable?

  init(store: PhotoStore) {
 = store
    stateWatcher = { [weak self] state in
      self?.state = state
    sideEffectWatcher = { [weak self] state in
      self?.sideEffect = state
  public func dispatch(_ action: PhotoAction) {
    store.dispatch(action: action)
  deinit {
Copy code
class MyPhotoApp: App {
  let photoRepository: PhotoRepositoryImpl
  let store: ObservablePhotoStore
  required init() {
    photoRepository = PhotoRepositoryImpl()
    store = ObservablePhotoStore(store: PhotoStore(photoRepository: photoRepository))
  var body: some Scene {
    WindowGroup {
I'm new to SwitUI and I try to learn from how to receive changes in StateFlow to update my Swift UI. I'm looking for a simple way to do that and that sample project is already very complex.
Eventually my goal is to throw my "" Array into a SwiftUI List that should be re-rendered if the contents change.

Stephen Gazzard

06/29/2021, 4:24 PM
What is

Stefan Oltmann

06/29/2021, 8:13 PM
Copy code
data class PhotoState(
    val progress: Boolean,
    val photos: List<Photo>,
    val selectedPhoto: Photo? = null,
    val testString: String
) : State
Copy code
interface State
interface Action
interface Effect

interface Store<STATE : State, ACTION : Action, EFFECT : Effect> {

    fun observeState(): StateFlow<STATE>

    fun observeSideEffect(): Flow<EFFECT>

    fun dispatch(action: ACTION)

In the original code from @Kathrin Petrova this seems to work somehow, but not for me. I'm puzzled.