UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Sectioned list from @FetchRequest?

Forums > SwiftUI

I posted this last week about some problems I've been facing when deleting items from a sectioned list. I'm fairly confident after giving it some thought that the issue here is that I'm refreshing two independent sources of truth, which puts a dead-end in place for this approach to mutating my FetchedResults after the fetch. Which leaves me with the question…

Is there a better (right?) way to to grouping FetchedResults so that they can be rendered in a sectioned list?

Thanks!

2      

hi Ben,

i've started to write a reply a few times and stopped, because i really don't see anything obviously amiss with your sectioning code.

i have used a similar setup, where the outer List is determined with a ForEach using a function of the @FetchRequest, and the function's return is either a 2-D array of items, or a 1-D array of custom structs (e.g., Array<SectionData>, where SectionData might have an id, the section's title, and the array of items for the section). i've not seen such a crazy animation for deletions before in either of these cases.

FWIW, i have had a constant struggle working with @FetchRequest and Core Data deletions, both when it comes to visual updates and the occasional crash with nil references to deleted Core Data objects. this happened even with a single section list.

so i have gone a slightly different route: i'll do a simple Core Data fetch() of all the items for the View and load them into a @Published array in an @ObservedObject (some sort of "viewModel") when the View comes on screen (usually, only the first time it comes on screen). then i do notifications when items are to be edited, added, or deleted; the viewModel picks up those notifications and makes an appropriate adjustment to its items array; and because this changes the items array, the View redraws using the viewModel's breakout of the items by section.

if you're interested in an example, take a look at my Shopping List project on Github. there are plenty of comments scattered around in the code to help folks understand what i did and why i did it.

hope that helps,

DMG

3      

I took a different approach with my sections. In my Entity, there is an attribute called type (3 choices, Time, Timer & Location)

Here is the section for Time. There are 2 more identical for the other types.

Text("Time")
      .foregroundColor(Color("Label"))
      .padding(.top, 8)
      List {
         ForEach(getTimeEvents(), id: \.self) { event in

         TimeCell(event: event)
            .frame(height: 52)
            .onTapGesture {
               self.selectedEvent = event
               self.showDetail.toggle()
            }
         }.onDelete { indices in
            self.getTimeEvents().delete(at: indices, from: self.viewContext)

     }

Where I sort for each section by using (there are 2 more of these for Timer & Location). They all use the normal fetchrequest var events.

func getTimeEvents() -> [Event] {
        return events.filter() { $0.type == .Time }
}

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.