[英]Understanding Core Data, passing data between views in SwiftUI
i'm try to create a little project using CoreData and SwiftUI我正在尝试使用 CoreData 和 SwiftUI 创建一个小项目
I have create 2 simple entity one called Airport with attribute icaoAPT and one called Briefing with attribute called note我创建了 2 个简单实体,一个名为 Airport,属性为 icaoAPT,另一个名为 Briefing,属性为 note
a relation between both of them, every airport should have many note两者之间的关系,每个机场都应该有很多注意事项
on the contentView I managed to create a list showing all the airport Inserted在 contentView 上,我设法创建了一个列表,显示所有插入的机场
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在 addview 我将数据添加到机场实体
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.在 detailsView 上我想显示与该机场相关的注释,但我编写的代码不起作用,我觉得我应该在 detailsView 上放置一个 NSFetch 来过滤该机场的注释....但我不知道如何写下来。
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:这是我的 AddNote 视图:
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谢谢您的帮助
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.如果您期望每个机场有很多注释,我建议您更改数据 model 以便它接受两者之间的“一对多”关系。
To answer the main question;回答主要问题; to be able to filter you need to filter the results based on which airport you select.
为了能够过滤您需要根据您 select 的机场过滤结果。 This article does a good job at explaining that and going more into detail using NSPredicate.
本文很好地解释了这一点,并使用 NSPredicate 进行了更详细的介绍。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.