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

List doesn't update after data changed

Forums > Swift

Hi,

I have a list that displays a breathing pattern. I have a function to reorder the data and change the .order property that has worked well in other places, but here the list doesn't get updated when an element is moved. This causes the move function to mess up the next time you move an item. Only when you press the back button and come back the order shows correctly. How can I make sure the list updates right away after an item has been moved?

It might have something to do with the @Binding variable. It is defined in the parent view like so: @State private var breathings = [Breathing]()

This is the code with the list:

import SwiftUI

struct BreathingPatternView: View {

    @Environment(\.managedObjectContext) private var viewContext
    @Environment(\.colorScheme) var colorScheme

    @Binding var breathings: Array<Breathing>

    @State private var add = false

    var body: some View {
        ZStack {
            GeometryReader { geo in
                Image("paper")
                    .resizable()
                    .scaledToFill()
                    .frame(width: geo.size.width)
                    .opacity(1)
                Rectangle()
                    .fill(colorScheme == .dark ? Color.darkBeige : Color.lightBeige).opacity(0.7)
                    .frame(width: geo.size.width, height: geo.size.height)
            }
            .edgesIgnoringSafeArea(.all)
            VStack {
                List {
                    ForEach (breathings.sorted(by: { $0.order < $1.order }), id: \.self) { breathing in
                        VStack {
                            Text("\(breathing.type!) \(breathing.time, specifier: "%.1f") seconds")
                            Text("order: \(breathing.order)")
                        }
                    }
                    .onDelete(perform: deleteBreath)
                    .onMove(perform: moveBreath)

                }

                .scrollContentBackground(.hidden)
                .toolbar {
                    ToolbarItem(placement:.navigationBarTrailing){
                        EditButton()
                    }
                    ToolbarItem(placement:.navigationBarTrailing){
                        Button(action: {
                            add.toggle()
                        }, label: {
                            Label("Add Breath",systemImage: "plus")
                        })

                    }
                }
            }
            .navigationTitle("Breathing pattern")
            .sheet(isPresented: $add){
                AddBreathView(breathings: $breathings)
            }
        }

    }

    private func deleteBreath(at offset:IndexSet){
        withAnimation {
            for index in offset{
                breathings.remove(at: index)
            }
        }
    }

    private func moveBreath(at sets:IndexSet,destination:Int) {
        let itemToMove = sets.first!
        if itemToMove < destination {
            var startIndex = itemToMove + 1
            let endIndex = destination - 1
            var startOrder = breathings[itemToMove].order
            while startIndex <= endIndex {
                breathings[startIndex].order = startOrder
                startOrder = startOrder + 1
                startIndex = startIndex + 1
            }
            breathings[itemToMove].order = startOrder
        } else if destination < itemToMove {
            var startIndex = destination
            let endIndex = itemToMove - 1
            var startOrder = breathings[destination].order + 1
            let newOrder = breathings[destination].order
            while startIndex <= endIndex {
                breathings[startIndex].order = startOrder
                startOrder = startOrder + 1
                startIndex = startIndex + 1
            }
            breathings[itemToMove].order = newOrder
        }

    }

}

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.