This is my definition of Model for SwiftData:
import SwiftData
@Model
class TerritoryGroup {
var name: String?
var territories: [Territory]?
init(name: String) {
self.name = name
}
}
@Model
class Territory {
var name: String?
var territoryGroup: TerritoryGroup?
init(name: String) {
self.name = name
}
}
Simple and nothing complicated. In the CloudKit I have 20 models, and Territory and TerritoryGroup have a few more properties saved in CloudKit. I don't know if it matters, but I just mapped two of them to check how it works. I am pretty sure on the CloudKit store there is one TerritoryGroup object and 9 Territory objects.
This is how I defined the store:
import SwiftData
import SwiftUI
@main
struct FieldService: App {
var sharedModelContainer: ModelContainer = {
let schema = Schema([
TerritoryGroup.self,
Territory.self,
])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
do {
return try ModelContainer(for: schema, configurations: [modelConfiguration])
} catch {
fatalError("Could not create ModelContainer: \(error)")
}
}()
@UIApplicationDelegateAdaptor(ApplicationDelegate.self) var viewappDelegate
var body: some Scene {
WindowGroup {
StartView()
}
.modelContainer(sharedModelContainer)
}
}
How do I display that in my App?
import CoreLocation
import SwiftUI
struct StartView: View {
@Environment(\.modelContext) private var modelContext
@Query private var territoryGroups: [TerritoryGroup]
@Query private var territories: [Territory]
var body: some View {
List{
ForEach (territoryGroups) { item in
Text(item.name ?? "abc")
}
ForEach (territories) { item in
Text(item.name ?? "def")
}
}
}
}
And the result is:
So simply it displays default values, not the real ones. Why?
Of course I have my .xcdatamodeld in project. I am not sure if it is needed any more, but it is. Of course with all 20 Entities

anythingin the models properties since you setup your project. That is, without propermigration.Model? Of course I have created only 2SwiftDataModels, but in CloudKit there is a lot more, and in xcdatamodeld file, too. Is it the reason?@Relationshipproperty wrapper.did you change anything in the models properties since you setup your projectis clearer now. You want to use a new@Model classwith fewer properties than your original stored data. AFAIK, you cannot declare a new@Model classthat only maps to a subset of the stored properties. Use full original@Model classand only read properties you need, ignore the rest.