Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwiftUI views don't get reused properly #154

Open
bryankeller opened this issue Sep 30, 2023 · 0 comments
Open

SwiftUI views don't get reused properly #154

bryankeller opened this issue Sep 30, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@bryankeller
Copy link
Contributor

bryankeller commented Sep 30, 2023

When using SwiftUI views in an Epoxy Collection View, views are reused and onAppear is called for each one correctly most of the time, but not if you scroll up and down really fast (causing very fast cell reuse). See video:

Simulator.Screen.Recording.-.iPhone.14.Pro.-.2023-09-29.at.17.19.22.mp4

Repro case:

import Epoxy
import SwiftUI
import UIKit

final class SwiftUIInEpoxyViewController: CollectionViewController {

  init() {
    super.init(layout: UICollectionViewCompositionalLayout.listNoDividers)
    setItems(items, animated: false)
  }

  private var items: [ItemModeling] {
    (1...100).map { (index: Int) in
      SwiftUIView(value: index).itemModel(dataID: index)
    }
  }
}

private final class Store: ObservableObject {
  @Published var value: Int?
}

private struct SwiftUIView: View {

  let value: Int

  @StateObject private var store = Store()

  var body: some View {
    HStack {
      Text("\(value)")
      if let storeValue = store.value {
        Spacer()
        Text("\(storeValue)").foregroundColor(storeValue == value ? .black : .red)
      }
    }
    .padding(.horizontal)
    .onAppear {
      store.value = value
    }
  }

}
@bryankeller bryankeller added the bug Something isn't working label Sep 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant