Search code examples
core-dataswiftuiapple-watchwatchos

Core Data, Problem with updating (duplicating instead)


I am new to Swift UI. Could you please help me with core data updating? Here is the point of a problem:

I am building a WatchOS app. There are 3 Views there:

  1. FirstView - a view with a button to Add a new Goal and a List of added Goals.
  2. AddGoalView - appears after pressing Add new Goal.
  3. RingView - a view with a Goal Ring (similar to activity ring mechanics) and all the data presented.

The point of the problem is the next:

  • After adding a new Goal everything is alright. The Data passes correctly from AddGoalView to the FirstView. I need only 2 items to be passed out of AddGoalView (One String and one Double).
  • Then, after pressing on the recently created Goal I appear on the Ring View. I successfully pass there 2 items (that String and Double I mentioned).
  • On the RingView I want to update the 3-rd Item (double) and send it back. So it can be updated on the FirstView.

the Result: Instead of updating this 3-d Item it just seems to create a completely new Goal on the First View below the previous Goal. Photo

My Code (FirstView):

struct FirstView: View {
    @FetchRequest (
        entity:NewGoal.entity(),
        sortDescriptors:[NSSortDescriptor(keyPath: \NewGoal.dateAdded, ascending: false)],
        animation: .easeInOut )
    
    var results:FetchedResults<NewGoal>
    @State var showMe = false
    
    var body: some View {
        ScrollView{
            VStack{
                VStack(alignment: .leading){
                    Text("My Goals:")
                    NavigationLink(
                        destination: AddGoalView(),
                        isActive: $showMe,
                        label: {
                            Image(systemName: "plus")
                            Text("Set Money Goal")
                        })
                    
                    Text("Recents:")
                    ForEach(results){ item in
                        VStack(alignment: .leading){
                            NavigationLink(
                                destination: RingView(GTitle: item.goalTitle ?? "", Sum: item.neededSum),
                                label: {
                                    HStack{
                                        Image(systemName: "gear")
                                        VStack(alignment: .leading){
                                            Text(item.goalTitle ?? "")
                                            HStack{
                                                Text("$\(item.yourSum, specifier: "%.f")") ///This item doesn't update
                                                Text("/ $\(item.neededSum, specifier: "%.f")")
                                            }
                                        }
                                    }
                                })
                        }
                    }
                }
            }
        }
    }
}

My Code (AddGoalView):

struct AddGoalView: View {
    @State private var goalTitle = ""
    @State private var showMe:Bool = true
    @State private var neededSum:Double = 0.0
    @State private var isFocusedNum = false
    @Environment(\.managedObjectContext) var context
    @Environment(\.presentationMode) var presentationMode
    var body: some View {
        ScrollView{
            VStack (alignment: .leading, spacing: 6){
                TextField("Goal Name...", text: $goalTitle)
                HStack{
                    Text("$\(neededSum, specifier: "%.f")")
                        .overlay(
                            RoundedRectangle(cornerRadius: 9)
                                .stroke(isFocusedNum ? Color.red : Color.white, lineWidth: 1)
                                .opacity(1.0))
                        .focusable(true) { newState in isFocusedNum = newState}
                        .animation(.easeInOut(duration: 0.1), value: isFocusedNum)
                        .digitalCrownRotation(
                            $neededSum,
                            from: 0,
                            through: 100000,
                            by: 25,
                            sensitivity: .high)
                }
                Button(action: addGoal) {
                    Text("Add Goal")
                }
                .disabled(neededSum == 0.0)
                .disabled(goalTitle == "")
                .navigationTitle("Edit")
            }
        }
    }
    private func addGoal(){
        let goal = NewGoal(context: context)
        goal.goalTitle = goalTitle
        goal.dateAdded = Date()
        goal.neededSum = neededSum
        do{
            try context.save()
            presentationMode.wrappedValue.dismiss()
        }catch let err{
            print(err.localizedDescription)
        }
    }

My Code (RingView Code):

struct RingView: View {
    @State private var isFocusedSum = false
    @State private var yournewSum:Double = 0.0
    var goalItem: NewGoal?
    var Sum:Double
    var GTitle:String
    @Environment(\.managedObjectContext) var context
    @Environment(\.presentationMode) var presentationMode
    @FetchRequest var results: FetchedResults<NewGoal>
    init(GTitle: String, Sum: Double){
        self.GTitle = GTitle
        self.Sum = Sum
        let predicate = NSPredicate(format:"goalTitle == %@", GTitle)
        self._results=FetchRequest(
            entity: NewGoal.entity(),
            sortDescriptors: [NSSortDescriptor(keyPath: \NewGoal.dateAdded, ascending: false)],
            predicate: predicate,
            animation: .easeInOut
        )
    }
    
    var body: some View {
        ZStack{
            ForEach(results) { item in
                RingShape(percent:(yournewSum/item.neededSum*100), startAngle: -90, drawnClockwise: false) /// Ring
                    .stroke(style: StrokeStyle(lineWidth: 10, lineCap: .round))
                    .fill(AngularGradient(gradient: Gradient(colors: [.red, .pink, .red]), center: .center))
                    .frame(width: 155, height: 155)
                HStack(alignment: .top){
                    Spacer()
                    Button(action: addSum) { ///BUTTON TO Update
                        Image(systemName: "gear")
                    }
                    .clipShape(Circle())
                }
                VStack(alignment: .trailing, spacing: 0.0){
                    Spacer()
                    Text("$\(yournewSum, specifier: "%.f")")     /// Here is the data I want to change via Digital Crown and update
                        .font(.title3)
                        .overlay(
                            RoundedRectangle(cornerRadius: 7)
                                .stroke(Color.white, lineWidth: 2)
                                .opacity(isFocusedSum ? 1.0:0.0)
                        )
                        .focusable(true) { newState in isFocusedSum = newState}
                        .animation(.easeInOut(duration: 0.3), value: isFocusedSum)
                        .digitalCrownRotation(
                            $yournewSum,
                            from: 0,
                            through: Double((item.neededSum)),
                            by: 10,
                            sensitivity: .high)
                    Text("/ $\(item.neededSum, specifier: "%.f")") ///Here is the Double data I entered in AddGoalView
                        .font(.caption)
                }
                .frame(width: 200, height: 230)
                .padding(.top, 7)
                VStack(alignment: .center, spacing: 1.0){
                    Text(item.goalTitle ?? "Your Goal Name") ///Here is the String data I entered in AddGoalView
                        .foregroundColor(.gray)
                }
                .padding(.top, 200.0)
            }
        }
        .padding([.top, .leading, .trailing], 5.0)
        
    }
    private func addSum(){
        let goal = goalItem == nil ? NewGoal(context: context): goalItem
        goal?.yourSum = yournewSum //// I am trying to update the Data here, but after running the func it creates a duplicate.
        do{
            try context.save()
            presentationMode.wrappedValue.dismiss()
        } catch let err{
            print(err.localizedDescription)
        }
    }

Solution

  • You never give var goalItem: NewGoal? the initial value of the item you want to update.

    try replacing this

    RingView(GTitle: item.goalTitle ?? "", Sum: item.neededSum)
    

    with

    RingView(goalItem: item, GTitle: item.goalTitle ?? "", Sum: item.neededSum)
    

    and of course your have to change your initializer for RingView to

    init(goalItem: NewGoal? = nil, GTitle: String, Sum: Double){
            
    

    and add to the initializer this line

    self.goalItem = goalItem