Search code examples
swiftxcodecore-dataswiftuinsfetchrequest

Understanding Core Data, passing data between views in SwiftUI


i'm try to create a little project using CoreData and SwiftUI

I have create 2 simple entity one called Airport with attribute icaoAPT and one called Briefing with attribute called note

a relation between both of them , every airport should have many note

Relation Entity Airport Briefig

on the contentView I managed to create a list showing all the airport Inserted

import SwiftUI

struct ContentView: View {
    @Environment(\.managedObjectContext) var managedObjectContext
    //Lista aeroporti
    @FetchRequest(
        entity: Airport.entity(),
        sortDescriptors: [ NSSortDescriptor(keyPath: \Airport.icaoAPT, ascending: true) ]
    ) var apt: FetchedResults<Airport>
    
    var body: some View {
        NavigationView {
            List{
                ForEach(apt, id: \.self) { airp in
                    NavigationLink(destination: DeatailsView(briefing: airp)) {
                        HStack{
                        Text(airp.icaoAPT ?? "Not Avail")
                        }
                    }
                }
            }.navigationBarTitle(Text("Aeroporti"))
                .navigationBarItems(trailing: NavigationLink(destination: AddView(), label: {
                    Text("add data")
                }))
        }
    }
}

on the addview I add data to the airport Entity

import SwiftUI

struct AddView: View {
    @State var aptICAO : String = ""
    @Environment(\.presentationMode) var presentation
    @Environment(\.managedObjectContext) var dbContext
    
    var body: some View {
        VStack{
            TextField("ICAO", text: self.$aptICAO)
            .textFieldStyle(RoundedBorderTextFieldStyle())
            Button(action: {
                
                let newAirport = Airport(context: self.dbContext)
                newAirport.icaoAPT = self.aptICAO
               
                do {
                    try self.dbContext.save()
                    self.presentation.wrappedValue.dismiss()
                    
                } catch {
                    print("errore nel salva")
                }
            }) {
                Text("Save")
            }
        }.padding()
        
    }
}

on the detailsView I want to show the note related to that airport, but the code I wrote not working, I feel I should put an NSFetch on the detailsView filtering the note for that airport.... but I don't know how to write it.

my detailsView:

import SwiftUI

struct DeatailsView: View {
    @Environment(\.managedObjectContext) var dbContext
    @Environment(\.presentationMode) var presentation
    @State var briefing : Airport
    @FetchRequest(
        entity: Briefing.entity(),
        sortDescriptors: []) var noteAPT: FetchedResults<Briefing>
    
    var body: some View {
        VStack{
            ForEach(noteAPT, id: \.self) { dt in
                Text(dt.note ?? "Nt avail")
            }
        }
        .navigationBarTitle( Text(briefing.icaoAPT ?? "apt not avail"))
        .navigationBarItems(trailing: NavigationLink(destination: AddNote(airport: briefing), label: {
            Text("add Note")
        }))
    }
}

here my AddNote view:

struct AddNote: View {
    @State var note : String = ""
       @Environment(\.presentationMode) var presentation
       @Environment(\.managedObjectContext) var dbContext
        @State var airport : Airport
    var body: some View {
        VStack{
            TextField("Note", text: self.$note)
                   .textFieldStyle(RoundedBorderTextFieldStyle())
                   Button(action: {
                       
                    let newNOTE = Briefing(context: self.dbContext)
                    newNOTE.note = self.note
                    
                       do {
                           try self.dbContext.save()
                           self.presentation.wrappedValue.dismiss()
                           
                       } catch {
                           print("errore nel salva")
                       }
                   }) {
                    Text("Save Note for\(airport.icaoAPT ?? "NA")")
                   }
               }.padding()
    }
}

I always see the same note I add for every airport , but should be different on each airport.

thanks for the help


Solution

  • If you are expecting many notes for each airport I would suggest changing your data model so that it accepts a 'one to many' relationship between the two.

    To answer the main question; to be able to filter you need to filter the results based on which airport you select. This article does a good job at explaining that and going more into detail using NSPredicate.