I have a TabView which consists of a few different tabs. One of which does an @Query to retrieve an array of Transaction models. These are then displayed in a list using a ForEach.
struct TransactionsTab: View {
@Query private var transactions: [Transaction]
... other code
Section(content: {
ForEach(transactions) { transaction in
transaction.getListItem()
}
}, header: {
LabeledView(label: "Recent Transactions", view: {
ListButton(mode: .link(destination: {
ListView(list: transactions)
.navigationTitle("All Transactions")
}))
})
})
Transaction contains a different model called TransactionItem and that has a variable called amount. That amount variable is used in the getListItem() function to show how much the total transaction was in the list item.
The issue is that I can delete a Transaction and the ForEach will update to reflect that. However, if I delete an TransactionItem separately, that getListItem() will not show that it's been deleted. The total amount shown will still be as if the TransactionItem was never deleted. It will only update when the app is closed and reopened. Below is the code that's ran when deleting a model, in this case a TransactionItem.
// Deletes a single item
private func delete() {
deleteWarning = false
if let item = itemToDelete {
// If last item is being delete, dismiss the view
if list.count == 1 { dismissView() }
context.delete(item)
context.saveContext()
itemToDelete = nil
}
mode = .view
}
I would think that deleting the model and having it save will cause the transaction query to update. What's going on here to cause it to not update?
By the way, saveContext() just calls the ModelContext save function.
extension ModelContext {
func saveContext() {
do {
try self.save()
} catch {
print("Could not save context: \(error.localizedDescription)")
}
}
}
Post
Replies
Boosts
Views
Activity
Hello. See the code below.
struct ContentView: View {
var body: some View {
TabView {
VehicleTab()
.tabItem({ Label("Vehicles", systemImage: "car.fill")})
.modelContainer(for: Vehicle.self)
TransactionsTab()
.tabItem { Label("Transactions", systemImage: "dollarsign") }
.modelContainer(for: Transaction.self)
}
}
}
Using the .modelContainer() in this way seems to be causing some issue. I was under the assumption that this would just create a container for each view. I get the error below in this configuration. If I comment out either one of the .modelContainer() modifiers, it works fine.
Query encountered an error: Error Domain=NSCocoaErrorDomain Code=256 "The file “default.store” couldn’t be opened."
Are you not able to do what I'm doing? Is there a way to have two separate containers?
Hello. I had an issue that I was able to resolve, but I'd like to see if anyone might know why it happened in the first place. Essentially, I have a SwiftData model called Vehicle. This model has an array of Mileage, another SwiftData model. Vehicle has an init that accepts a Vehicle as a parameter and then matches it with that one.
init(from vehicle: Vehicle) {
self.id = vehicle.id
self.timestamp = vehicle.timestamp
self.year = vehicle.year
self.make = vehicle.make
self.model = vehicle.model
self.trim = vehicle.trim
self.mileage = vehicle.mileage
self.nickname = vehicle.nickname
}
Previously, I had the line self.mileageHistory = vehicle.mileageHistory in this init. However, that line caused a duplicate Vehicle model to be created and inserted into the context. I could tell because I had a List that was displaying all of the created Vehicle models from a Query.
It all stems from a view that is being displayed in a sheet. This view accepts a vehicle parameter called copy.
.sheet(isPresented: $edit, content: { VehicleEditView(vehicle: vehicle, copy: Vehicle(from: vehicle)) })
In a way I can understand why it's happening because a new Vehicle model is being created. But I don't understand why it only happens when the mileageHistory variables are set equal to each other. I removed that line and it doesn't do it anymore. I had to workaround it by setting the mileageHistory elsewhere.
Does anyone know why this might be happening?