0

Every time I insert a subclass (MYShapeLayer) into the model context, the app crashes with an error:

DesignerPlayground crashed due to fatalError in BackingData.swift at line 908. Never access a full future backing data - PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(backing: SwiftData.PersistentIdentifier.PersistentIdentifierBacking.managedObjectID(0xb2dbc55f3f4c57f2 x-coredata://B1E3206B-40DE-4185-BC65-4540B4705B40/MYShapeLayer/p1))) with Optional(A6CA4F89-107F-4A66-BC49-DD7DAC689F77)`

struct ContentView: View {
  
  @Environment(\.modelContext) private var modelContext
  
  @Query private var designs: [MYDesign]
  
  var layers: [MYLayer] {
    designs.first?.layers ?? []
  }
  
  var body: some View {
    NavigationStack {
      List {
        ForEach(layers) { layer in
          Text(layer.description)
        }
      }
      .onAppear {
        let design = MYDesign(title: "My Design")
        modelContext.insert(design)
        try? modelContext.save()
      }
      .toolbar {
        Menu("Add", systemImage: "plus") {
          Button(action: addTextLayer) {
            Text("Add Text Layer")
          }
          
          Button(action: addShapeLayer) {
            Text("Add Shape Layer")
          }
        }
      }
    }
  }
   
  private func addTextLayer() {
    if let design = designs.first {
      let newLayer = MYLayer(order: layers.count, kind: .text)
      newLayer.design = design
      modelContext.insert(newLayer)
      try? modelContext.save()
    }
  }
  
  private func addShapeLayer() {
    if let design = designs.first {
      let newLayer = MYShapeLayer(shapeName: "Ellipse", order: layers.count)
      newLayer.design = design
      modelContext.insert(newLayer)
      try? modelContext.save()
    }
  }
}

#Preview {
  ContentView()
    .modelContainer(for: [MYDesign.self, MYLayer.self, MYShapeLayer.self], inMemory: true)
}

@Model
final class MYDesign {
  
  var title: String = ""
  
  @Relationship(deleteRule: .cascade, inverse: \MYLayer.design)
  var layers: [MYLayer] = []
  
  init(title: String = "") {
    self.title = title
  }
  
}

@available(iOS 26.0, macOS 26.0, *)
@Model
class MYLayer {
  
  var design: MYDesign!
  
  var order: Int = 0
  
  var title: String = ""
  
  init(order: Int = 0, title: String = "New Layer") {
    self.order = order
    self.title = title
  }
  
}

@available(iOS 26.0, macOS 26.0, *)
@Model
class MYShapeLayer: MYLayer {
  
  var shapeName: String = ""
  
  init(shapeName: String, order: Int = 0) {
    self.shapeName = shapeName
    super.init(order: order)
  }
}
10
  • “Never access a full future backing data” seems to be a common error for SwiftData inheritance, I was going to link to the Apple developer forums for similar issues but I saw that you have already posted there. Commented Sep 26 at 6:41
  • this SO post deals with inheritance stackoverflow.com/questions/79741327/… . My take is that SwiftData doesn’t support storing an array of heterogeneous model types. Commented Sep 26 at 7:09
  • I’m not sure the reason but Query should be for layers not designs. And btw Inheritance is designed for when you want one Query to return multiple model types like how Notes shows Accounts (all notes) and Folders in same list. Commented Sep 26 at 8:27
  • Note, in MyLayer you should have var design: MYDesign?, note the ?. Also you have one type MYShapeLayer and another type MYLayer. Two different types, trying to fit into the array var layers: [MYLayer] of MYDesign, this AFAIK is not supported in SwiftData. Commented Sep 26 at 8:42
  • @koen, unlike the question you mentioned, this is specifically about inheritance in SwiftData.. Commented Sep 26 at 10:33

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.