I have two models:
@Model
class User: Codable {
@Attribute(.unique) var username: String
@Attribute(.unique) var email: String
var firstName: String
var lastName: String
@Attribute(.unique) var id: Int
@Relationship(inverse: \House.members) var houses: [House] = []
init(username: String, email: String, firstName: String, lastName: String, id: Int) {
self.username = username
self.email = email
self.firstName = firstName
self.lastName = lastName
self.id = id
}
enum CodingKeys: String, CodingKey {
case username
case email
case firstName = "first_name"
case lastName = "last_name"
case id
}
required init(from decoder: Decoder) throws {
...
}
func encode(to encoder: Encoder) throws {
...
}
}
and
@Model
class House: Codable {
var name: String
var city: String
var address: String
@Attribute(.unique) var id: Int
var ownerID: Int
var members: [User]
init(name: String, city: String, address: String, id: Int, ownerID: Int, members: [User]) {
self.name = name
self.city = city
self.address = address
self.id = id
self.ownerID = ownerID
self.members = members
}
enum CodingKeys: String, CodingKey {
case name
case city
case address
case id
case ownerID = "owner_id"
case members
}
required init(from decoder: Decoder) throws {
...
}
func encode(to encoder: Encoder) throws {
...
}
}
I want to save a list of House objects I receive in JSON format:
[
{
"name": "A",
"city": "A",
"address": "A",
"id": 1,
"owner_id": 1,
"members": [
{
"username": "A",
"email": "A",
"first_name": "A",
"last_name": "A",
"id": 1
}
]
}
...
]
This is the code I use:
import SwiftUI
import SwiftData
struct HouseListView: View {
@Environment(\.modelContext) var modelContext
@Query(sort: \House.name) var houses: [House]
var body: some View {
NavigationStack {
List {
ForEach(houses) { house in
Text(house.name)
}
}
.task {
if let houses = await getHouses() {
do {
for house in houses {
modelContext.insert(house)
}
try modelContext.save()
} catch {
print(error)
}
}
}
}
}
}
But I receive the following errors:
Error Domain=NSCocoaErrorDomain Code=1560 "Multiple validation errors occurred." UserInfo={NSDetailedErrors=(
"Error Domain=NSCocoaErrorDomain Code=1570 \"%{PROPERTY}@ is a required value.\" UserInfo={NSValidationErrorObject=<NSManagedObject: 0x2827cc8c0> (entity: User; id: 0x2804967e0 <x-coredata:///User/tAC4F0253-65B0-4C92-846F-8E72A8E115277>; data: {\n email = nil;\n firstName = nil;\n houses = (\n );\n id = nil;\n lastName = nil;\n username = nil;\n}), NSLocalizedDescription=%{PROPERTY}@ is a required value., NSValidationErrorKey=email, NSValidationErrorValue=null}",
"Error Domain=NSCocoaErrorDomain Code=1570 \"%{PROPERTY}@ is a required value.\" UserInfo={NSValidationErrorObject=<NSManagedObject: 0x2827cc8c0> (entity: User; id: 0x2804967e0 <x-coredata:///User/tAC4F0253-65B0-4C92-846F-8E72A8E115277>; data: {\n email = nil;\n firstName = nil;\n houses = (\n );\n id = nil;\n lastName = nil;\n username = nil;\n}), NSLocalizedDescription=%{PROPERTY}@ is a required value., NSValidationErrorKey=firstName, NSValidationErrorValue=null}",
"Error Domain=NSCocoaErrorDomain Code=1570 \"%{PROPERTY}@ is a required value.\" UserInfo={NSValidationErrorObject=<NSManagedObject: 0x2827cc8c0> (entity: User; id: 0x2804967e0 <x-coredata:///User/tAC4F0253-65B0-4C92-846F-8E72A8E115277>; data: {\n email = nil;\n firstName = nil;\n houses = (\n );\n id = nil;\n lastName = nil;\n username = nil;\n}), NSLocalizedDescription=%{PROPERTY}@ is a required value., NSValidationErrorKey=id, NSValidationErrorValue=null}",
"Error Domain=NSCocoaErrorDomain Code=1570 \"%{PROPERTY}@ is a required value.\" UserInfo={NSValidationErrorObject=<NSManagedObject: 0x2827cc8c0> (entity: User; id: 0x2804967e0 <x-coredata:///User/tAC4F0253-65B0-4C92-846F-8E72A8E115277>; data: {\n email = nil;\n firstName = nil;\n houses = (\n );\n id = nil;\n lastName = nil;\n username = nil;\n}), NSLocalizedDescription=%{PROPERTY}@ is a required value., NSValidationErrorKey=lastName, NSValidationErrorValue=null}",
"Error Domain=NSCocoaErrorDomain Code=1570 \"%{PROPERTY}@ is a required value.\" UserInfo={NSValidationErrorObject=<NSManagedObject: 0x2827cc8c0> (entity: User; id: 0x2804967e0 <x-coredata:///User/tAC4F0253-65B0-4C92-846F-8E72A8E115277>; data: {\n email = nil;\n firstName = nil;\n houses = (\n );\n id = nil;\n lastName = nil;\n username = nil;\n}), NSLocalizedDescription=%{PROPERTY}@ is a required value., NSValidationErrorKey=username, NSValidationErrorValue=null}",
"Error Domain=NSCocoaErrorDomain Code=1570 \"%{PROPERTY}@ is a required value.\" UserInfo={NSValidationErrorObject=<NSManagedObject: 0x2827cca00> (entity: User; id: 0x280496a80 <x-coredata:///User/tAC4F0253-65B0-4C92-846F-8E72A8E115279>; data: {\n email = nil;\n firstName = nil;\n houses = (\n );\n id = nil;\n lastName = nil;\n username = nil;\n}), NSLocalizedDescription=%{PROPERTY}@ is a required value., NSValidationErrorKey=email, NSValidationErrorValue=null}",
"Error Domain=NSCocoaErrorDomain Code=1570 \"%{PROPERTY}@ is a required value.\" UserInfo={NSValidationErrorObject=<NSManagedObject: 0x2827cca00> (entity: User; id: 0x280496a80 <x-coredata:///User/tAC4F0253-65B0-4C92-846F-
.....
)}
ForEach<Array<House>, Int, Text>: the ID 3 occurs multiple times within the collection, this will give undefined results!