Posts

Post marked as solved
1 Replies
392 Views
Disclaimer: I first asked the question here - https://stackoverflow.com/questions/64215872/swiftui-how-do-i-update-a-list-that-is-driven-by-a-static-set-of-data-and-pulls/64231063?noredirect=1#comment113585930_64231063 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: [["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 - https://stackoverflow.com/a/60723054/554491. 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 var body: some View { &#9;DynamicFetchView(predicate: NSPredicate(format: "budgetId == %@ AND month == %ld AND year == %ld", budget.id as CVarArg, month, year), &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; sortDescriptors: []) { (budgetEntries: FetchedResults<BudgetCategoryEntry>) in &#9;&#9;VStack { &#9;&#9;&#9;List { &#9;&#9;&#9;&#9;ForEach(budget.sections) { section in &#9;&#9;&#9;&#9;&#9;if !section.hidden { &#9;&#9;&#9;&#9;&#9;&#9;Section(header: BudgetSectionCell(section: section, &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;amountText: "", &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;color: 0 < 0 ? Color("negativeAmount") : Color("positiveAmount")) &#9;&#9;&#9;&#9;&#9;&#9;) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;ForEach(section.categories) { category in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;if !category.hidden { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;BudgetCategoryCell(category: category, &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; amountText: String(format: "= %@ =", budgetEntries.first(where: { $0.budgetCategoryId == category.id })?.amount ?? -1 ), &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; color: 0 < 0 ? Color("negativeAmount") : Color("positiveAmount") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;&#9;} &#9;&#9;} &#9;} } 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 - https://stackoverflow.com/questions/64215872/swiftui-how-do-i-update-a-list-that-is-driven-by-a-static-set-of-data-and-pulls 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.
Posted
by morpheu5.
Last updated
.