0
class Model: ObservableObject {
   @Published var players: [Player] = []
}

struct PlayerListView: View {
    @ObservedObject var model: Model

    var body: some View {
        List(model.players, id: \.score) { player in
            HStack {
                Text("Name: \(player.name)")
                Text("Score: \(player.score)")
                Text("Status: \(player.status)")
                Text("Salary: \(player.salary)")
            }
        }
    }
}

struct Player: Codable {
   let name: String
   let score: Int
   let salary: Int
   let status: String
}

so the problem is, if I am performing any operation that increase or decrease the count of players array then view is getting refreshed as expected, but when something internal gets changed like Name, score or any thing by some operations like api calls view is not getting refreshed can anyone please help me out

these solutions are not working in ios14 or above self.objectWillChange.send()

6
  • 1
    "when something internal gets changed like name" How would that ever happen if name is let? Commented Nov 7, 2023 at 10:23
  • it is used as response model for api call Commented Nov 7, 2023 at 10:38
  • try using: struct Players: Codable { var name: String // var .... } Note the var and the struct not Struct Commented Nov 7, 2023 at 12:02
  • My guess would be that you are dealing with multiple sources of truth but you haven’t included relevant code to your issue. Commented Nov 7, 2023 at 12:15
  • @loremipsum, the problem is in big project but the situation is same Commented Nov 8, 2023 at 11:47

1 Answer 1

0

Score isn't unique enough to be a ForEach id so change it to:

struct Player: Identifiable {
    let id = UUID() // or var id: String { name } if you enforce unique names.

And

List(model.players) { player in
Sign up to request clarification or add additional context in comments.

7 Comments

no that is not the situation actually internal changes not refreshing the ui
Internal changes should work, e.g.a change to player will be detected as a change to the array and published should send objectWillChange which causes @ObservedObject to call body. Do you have anything else in your Player struct that might be breaking the check for a difference in Player equality?
No when the count is getting increased or decreased it is getting detected and refresh the ui but when the count is same and lets say player salary got changed it is not refreshing the ui
Are you doing model.players[0].salary = 50 ? You need to be careful with struct values
no i m calling change status api which changes to status, but when status got changed ui is not getting refreshed, for refreshing the ui i need to clear array first then i again need to call to get players api which refresh the ui same issue is here as well stackoverflow.com/questions/62461199/…
|

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.