Post

Replies

Boosts

Views

Activity

SwiftUI sheet accentColor
I use the .accentColor() modifier to apply a color to the entire application. At some point this method no longer seems to work with a sheet, where Color.accentColor returns the default blue color. It used to work, so I don't know what the reason is. Any solutions?
1
0
925
Jul ’22
App freezes when tapping a navigation link
I have two main models, House and Expense. On the home page I show a list of the three most recent expenses using the following query: var descriptor = FetchDescriptor<Expense>(predicate: #Predicate { $0.houseID == house.id }, sortBy: [SortDescriptor(\.date, order: .reverse)]) descriptor.fetchLimit = 3 _recentExpenses = Query(descriptor) I have a NavigationLink that takes you to the full list of expenses: NavigationLink { ExpenseListView(house: house) } label: { Text("See all") } On this page I have a query similar to the previous one, without the fetch limit: let descriptor = FetchDescriptor<Expense>( predicate: #Predicate { $0.houseID == house.id} ) _expenses = Query(descriptor) The problem is that when I click on the navigation link to go to the expenses page, the app freezes. I tried passing the expenses array to the second page instead of invoking another query, but this way the view doesn't update when I update the expenses. Is there a way to solve this problem? Or a way to pass a Binding of the fetched results?
4
1
1.1k
Apr ’24
SwiftData errors when trying to insert multiple objects
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!
3
3
1.8k
Sep ’23