That is my definios of Mannequin for SwiftData:
import SwiftData
@Mannequin
class TerritoryGroup {
var identify: String?
var territories: [Territory]?
init(identify: String) {
self.identify = identify
}
}
@Mannequin
class Territory {
var identify: String?
var territoryGroup: TerritoryGroup?
init(identify: String) {
self.identify = identify
}
}
Easy and nothng difficult. Within the CloudKit I’ve 20 fashions, and Territory and TerritoryGroup have just a few extra properties saved in CloudKit. I dont know if it issues, however I simply mapped two of them to test the way it works. I’m fairly certain on the cloudkit retailer there may be one TerritoryGroup object and 9 Territory
objects.
That is how I outlined the shop:
import SwiftData
import SwiftUI
@important
struct FieldService: App {
var sharedModelContainer: ModelContainer = {
let schema = Schema([
TerritoryGroup.self,
Territory.self,
])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
do {
return attempt ModelContainer(for: schema, configurations: [modelConfiguration])
} catch {
fatalError("Couldn't create ModelContainer: (error)")
}
}()
@UIApplicationDelegateAdaptor(ApplicationDelegate.self) var viewappDelegate
var physique: some Scene {
WindowGroup {
StartView()
}
.modelContainer(sharedModelContainer)
}
}
How do I show that in my App?
import CoreLocation
import SwiftUI
struct StartView: View {
@Surroundings(.modelContext) personal var modelContext
@Question personal var territoryGroups: [TerritoryGroup]
@Question personal var territories: [Territory]
var physique: some View {
Checklist{
ForEach (territoryGroups) { merchandise in
Textual content(merchandise.identify ?? "abc")
}
ForEach (territories) { merchandise in
Textual content(merchandise.identify ?? "def")
}
}
}
}
And the result’s:
So merely it shows default values. Not the true ones. Why?
After all I’ve my .xcdatamodeld
in challenge. I’m not certain whether it is wanted anymore, however it’s. After all with all 20 Entities