Home IOS Development ios – SwiftUI: Grandparent View Not Updating on Mannequin Change in SwiftData

ios – SwiftUI: Grandparent View Not Updating on Mannequin Change in SwiftData

0
ios – SwiftUI: Grandparent View Not Updating on Mannequin Change in SwiftData

[ad_1]

I am engaged on a SwiftUI app utilizing SwiftData for state administration. I’ve encountered a problem with view updates when mutating a mannequin. Here is a minimal instance as an instance the issue:

import SwiftUI
import SwiftData

// MARK: - Fashions

@Mannequin
class A {
  @Relationship var bs: [B] = [B]()
  
  init(bs: [B]) {
    self.bs = bs
  }
}

@Mannequin
class B {
  @Relationship var cs: [C] = [C]()
  
  init(cs: [C]) {
    self.cs = cs
  }
}

@Mannequin
class C {
  var completed: Bool
  
  init(completed: Bool) {
    self.completed = completed
  }
}

// MARK: - Views

struct CView: View {
  var c: C
  
  var physique: some View {
    @Bindable var c = c
    HStack {
      Toggle(isOn: $c.completed, label: {
        Textual content("Carried out")
      })
    }
  }
}

struct BView: View {
  var b: B
  
  var physique: some View {
    Record(b.cs) { c in
      CView(c: c)
    }
  }
}

struct AView: View {
  var a: A
  
  var physique: some View {
    Record(a.bs) { b in
      NavigationLink {
          BView(b: b)
      } label: {
        Textual content("B (b.cs.allSatisfy({ $0.completed }).description)")
      }
    }
  }
}

struct ContentView: View {
  @Question personal var aList: [A]
  
  var physique: some View {
    NavigationStack {
      Record(aList) { a in
        NavigationLink {
          AView(a: a)
        } label: {
          Textual content("A (a.bs.allSatisfy({ $0.cs.allSatisfy({ $0.completed }) }).description)")
        }
      }
    }
  }
}

@important
struct Minimal: App {
  var physique: some Scene {
    WindowGroup {
      ContentView()
    }
  }
}

#Preview {
  let config = ModelConfiguration(isStoredInMemoryOnly: true)
  let container = attempt! ModelContainer(for: A.self, configurations: config)
  
  var c = C(completed: false)
  container.mainContext.insert(c)
  var b = B(cs: [c])
  container.mainContext.insert(b)
  var a = A(bs: [b])
  container.mainContext.insert(a)
  
  return ContentView()
    .modelContainer(container)
}

On this setup, I’ve a CView the place I toggle the state of a mannequin C. After toggling C and navigating again, the grandparent view AView doesn’t mirror the up to date state (it nonetheless reveals false as a substitute of true). Nonetheless, if I navigate again to the basis ContentView after which go to AView, the standing is up to date accurately.

Why does not AView replace instantly after mutating C in CView, however updates accurately when navigating again to the basis ContentView? I anticipated the grandparent view to mirror the adjustments instantly as per SwiftData’s statement mechanism.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here