0

I'm able to save objects in CoreData. My Entity attribute is of type string and saves whatever is typed into a textfield. I'm able to update the attribute using FetchedEntityRequest[index] of whatever index I specify but i'm having trouble saving the selected attribute being passed into the DeatailView. please see below for example code.

//save Attribute
    func saveButtonTapped(text: String?) {
        if let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext {
            let Entity.attribute: String = Entity(context: context)
            Entity.attribute: = text
            if context.hasChanges {
                do {
                    try context.save()
                } catch {
                    print(error.localizedDescription)
                }
            }

 // pass attribute to DetailView from List View
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(
entity: Entity.entity(),
sortDescriptors: [
NSSortDescriptor(keyPath: \Entity.attribute, ascending: true)
]
) var fetchedEntityRequest: FetchedResults<Entity>
var body: some View {
    NavigationView {
        List {
            ForEach(fetchedEntityRequest, id: \.self) { entity in
                VStack {
                    HStack {
                    
                    Text(entity.attribute ?? "")
                        .font(.headline)
                    Spacer()
                    NavigationLink(destination: EditLyricsView(text: entity.attribute ?? "")) {
                    }
                }
                .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
            
        

        //DetailView - updating the index specified and saving changes to coredata.
    

@State var text: String = ""
        @Environment(\.managedObjectContext) var managedObjectContext
        @FetchRequest(
            entity: Entity.entity(),
            sortDescriptors: [
                NSSortDescriptor(keyPath: \Entity.attribute, ascending: true)
            ]
        ) var fetchedEntityRequest: FetchedResults<Entity>
        var body: some View {
            NavigationView {
                VStack {
                    TextView(text: $text)
                }  .navigationBarTitle(Text("self.text"), displayMode: .inline)
                .navigationBarItems(leading: Button("Home") {
                    presentationMode.wrappedValue.dismiss()
                    
                }, trailing: Button("Save") {
                    fetchedEntityRequest.forEach { i in
                        var index: Int = 0
                        fetchedEntityRequest[index].setValue(text, forKey: "Entity.attribute")
                        presentationMode.wrappedValue.dismiss()
                    }
                }
            }
 
b_swift20
  • 11
  • 3
  • Don't pass only attribute, pass in details entire object so you'll be able to save it after attribute modified. – Asperi Feb 04 '21 at 04:18

1 Answers1

0

Thanks Asperi! - That worked. see below for updates.

NavigationLink(destination: DetailedView(text: entity.attribute ?? "", entity: Entity)) {
}

I created an @Stated var on my DetailsView of type Entity? and called the below.

Entity?.setValue(text, forKey: entity.attribute)
b_swift20
  • 11
  • 3