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

Animation issue when deleting from sectioned list backed by Core Data

Forums > SwiftUI

Hi! I have a list of items from Core Data, which I'm grouping by date. Grouping and ordering is all working as expected, but when I delete an item, there's an extremely broken feeling animation–almost as though the entire list is being refreshed with the new data source, rather than just the deleted item being refreshed.

Does this look like a swiftui animation bug, or is there something in my grouping logic that might be causing it? Video of the issue. Any help appreciated. Thanks!

struct ContentView: View {
    @Environment(\.managedObjectContext) var moc

    @FetchRequest(entity: Entry.entity(), sortDescriptors: [
        NSSortDescriptor(keyPath: \Entry.date, ascending: false)
    ]) var entries: FetchedResults<Entry>

    @State var showingAddEntrySheet = false

    var dateFormatter: DateFormatter {
        let formatter = DateFormatter()
        formatter.dateStyle = .short
        return formatter
    }

    func groupedEntries(_ result: FetchedResults<Entry>) -> [[Entry]] {
        return Dictionary(grouping: result) { (element: Entry) in
            dateFormatter.string(from: element.date!)
        }.values.sorted() { $0[0].date! > $1[0].date! }
    }

    var body: some View {
        NavigationView {
            List {
                ForEach(groupedEntries(entries), id: \.self) { (section: [Entry]) in
                    Section(header: Text(self.dateFormatter.string(from: section[0].date!))) {
                        ForEach(section, id: \.self) { entry in
                            NavigationLink(destination: EntryDetailView(entry: entry), label: {
                                VStack(alignment: .leading) {
                                    Text(entry.exercise?.name ?? "No exercise name")
                                        .font(.headline)
                                        .foregroundColor(.primary)
                                    Text(entry.notes ?? "No notes")
                                        .font(.body)
                                        .foregroundColor(.secondary)
                                }
                            })
                        }
                        .onDelete { row in
                            deleteEntry(row: row, in: section)
                        }
                    }
                }
            }
            .listStyle(InsetGroupedListStyle())
            .navigationTitle("Entries")
            .navigationBarItems(trailing:
                Button("Add") {
                    self.showingAddEntrySheet.toggle()
                }
            )
        }
        .sheet(isPresented: $showingAddEntrySheet) {
            ExerciseListView(showingAddEntrySheet: $showingAddEntrySheet)
                .environment(\.managedObjectContext, self.moc)
        }
    }

    func deleteEntry(row: IndexSet, in section: [Entry]) {
        let entry = section[row.first!]
        print(section)
        print(entry)
        self.moc.delete(entry)

        do {
            try self.moc.save()
        } catch {
            print(error)
        }
    }
}

4      

Having the exact same problem. Have you found a solution in the meantime?

3      

Hi ! Same problem here, but only when I delete the last item of a section. Have you found any workaround/solution ?

3      

Funny; I hadn't noticed this until I just checked in my app. Turns it does this for me, too. But also just for my grouped list view. Others are fine.

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.