1

When adding to or removing from an array, the view does not update unless the user interacts with the ScrollView.

The Model is a ObservableObject that is declared as a StateObject early in the app lifecycle and then passed as a EnvironmentObject.

The data is simply a custom Profile object with an array of objects, when adding to store.profile!.tasks.append() or removing from the view does not update unless the user scrolls the ScrollView; I mean literally by 1 pixel.

What I have tried

  • Wrapping the ForEach in a LazyVStack or VStack
  • Wrapping the NavigationLink in a VStack
  • Making sure size is full height incase it needed to recalculate

Code

class Profile: Identifiable, Codable, Equatable
    var tasks: [Task]
}


struct Task: Identifiable, Codable, Equatable {
    var createdBy: String
    var title: String
    var date: Date
}

class Store : ObservableObject {
    @Published var profile: Profile?
}

struct ListView: View {

@EnvironmentObject var store: Store

  var body: some View {
     GeometryReader { geometry in
        ZStack(alignment: .bottomTrailing) {
          ScrollView(.vertical, showsIndicators: false){
              ForEach(store.profile!.tasks.filter({ return $0.createdBy == store.profile!.uid}).indices, id: \.self) { index in
                 NavigationLink(destination: DetailView().environmentObject(store)) {
                      TasksRow().id(UUID()) 
                 }
             }
          }
           .frame(maxWidth: geometry.size.width, alignment: .center)
           .frame(maxHeight: geometry.size.height)
           .background(Color.gray)
       }
    }
 }

1 Answer 1

2

Your Profile is a reference type, so when you append task in it the reference to profile is not changed, so published does not work.

Use instead value type for profile (ie. struct)

struct Profile: Identifiable, Codable, Equatable
    var tasks: [Task]
}

now appending task to profile will change profile itself, the publisher send event and view will refresh.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.