How do I update a List that is driven by a static set of data and pulls bits of information from another dynamic set of data?

Disclaimer: I first asked the question here but didn't go very far. I am not even sure that what I'm doing is the right approach so I'm very open to any suggestions.

To summarise: I have a View that contains a List that shows sections and rows based on a structure. This structure is static and is basically a "frame" on an underlying dataset managed by Core Data. More concretely: the List shows a bunch of budget categories that "never" change, and Core Data holds the "monthly" instances of these categories -- let's call them entries. So, if the "static" category is "Eating out", Core Data contains something like this:

Code Block swift
[["month":9,"year":2020,"amount":100],["month":10,"year":2020,"amount":200],["month":11,"year":2020,"amount":50]]

(massively simplified, but you get the idea).

After much gnawing of teeth, I ended up with a solution that actually pulls the entries from Core Data. This involves using the DynamicFetchView from this post. Even though it looks like an overkill, it works, in that every time I change any of the parameters of the predicate (month, year, budget id), the entries I get from Core Data change. So now I have something like

Code Block swift
var body: some View {
DynamicFetchView(predicate: NSPredicate(format: "budgetId == %@ AND month == %ld AND year == %ld", budget.id as CVarArg, month, year),
sortDescriptors: []) { (budgetEntries: FetchedResults<BudgetCategoryEntry>) in
VStack {
List {
ForEach(budget.sections) { section in
if !section.hidden {
Section(header: BudgetSectionCell(section: section,
amountText: "",
color: 0 < 0 ? Color("negativeAmount") : Color("positiveAmount"))
) {
ForEach(section.categories) { category in
if !category.hidden {
BudgetCategoryCell(category: category,
amountText: String(format: "= %@ =", budgetEntries.first(where: { $0.budgetCategoryId == category.id })?.amount ?? -1 ),
color: 0 < 0 ? Color("negativeAmount") : Color("positiveAmount")
)
}
}
}
}
}
}
}
}
}


I verified this by violently sticking a breakpoint on line 5 there to run p budgetEntries.map { $0.amount } every time the view is refreshed. It seems to print the right thing. Values for the combinations of parameters where the entries exist, and an empty array where the predicate returns no entries. All good.

Except that the content of the rows doesn't change. And by "content of the rows" I mean the number I pull in on line 15 above. The numbers stay the same as whatever was loaded when the view first appeared.

At this point, I would appreciate any help. If you have the patience to go through the SO question I originally posted, there is the whole process I went through to get where I am now, so you have an idea of what I tried and how I failed.

Accepted Reply

I have published the source code of the project, it's at this URL which I apparently can't include as a link… git.morpheu5.net/Morpheu5/yDnab-iOS

EDIT UGH, this is NOT the answer, I clicked on the badge by mistake, how do I unmark it?!

Replies

I have published the source code of the project, it's at this URL which I apparently can't include as a link… git.morpheu5.net/Morpheu5/yDnab-iOS

EDIT UGH, this is NOT the answer, I clicked on the badge by mistake, how do I unmark it?!