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

SwiftUI with CoreData

Forums > SwiftUI

First off, great tutorials and info. I followed a tutorial on Swift UI and CoreData and I have a toggle in the cell that completes the task. The issue I have is that if I only have one item saved in CoreData the view does refresh. I played around with creating an observable object but no luck. Anyways, if anyone has a suggestion on what I am doing wrong I would appreciate it. https://github.com/debratton/SwiftUI-ToDo

I figured the github link was better than pasting a bunch of code?

Thanks.

2      

To be more specific, in included a screenshot ![https://github.com/debratton/SwiftUI-ToDo/blob/master/Simulator%20Screen%20Shot%20-%20iPhone%2011%20-%202020-03-19%20at%2017.36.12.png]

This works great when there are two items in core data. If there is only one, I have to click the toggle 3 times before fetchrequest or view updates. I can click once and the toggle changes, but doesn't move to completed segment without switching to that segment.

            CategoryCompleteToggle(passedCategory: passedCategory, isOn: $isOn)
        } // End HStack
        Text(catName)
    } // End VStack
        .padding(10)
}

}

struct CategoryCompleteToggle: View {
    var passedCategory: Category
    @Environment(\.managedObjectContext) var moc
    @ObservedObject var catToggle = CategoryToggle()
    @Binding var isOn: Bool
var body: some View {
    VStack(alignment: .trailing) {
        Toggle(isOn: $isOn) {
            EmptyView()
        }
        .onAppear {
            self.isOn = self.passedCategory.catCompleted
            print("ONLOAD ISON VALUE: \(self.isOn)")
        }
        .onTapGesture {
            self.catToggle.catToggleOn = !self.isOn
            print("IsOn Value: \(self.isOn)")
            print("Passed Category Name: \(self.passedCategory.catName ?? "Unknown")")
            print("Current Completed Value: \(self.passedCategory.catCompleted)")
            self.passedCategory.catCompleted = !self.isOn
            do {
                try self.moc.save()
            } catch {
                print(error.localizedDescription)
            }

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.