WWDC21 SALE: Save 50% on all my Swift books and bundles! >>

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!

   

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

1      

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 }
}

   

Save money with our WWDC sale!

SAVE 50% To celebrate WWDC21, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

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.