我正在努力弄清楚我可能做错了什么(或者,当然,对关系、抓取等等的误解)。
在第一次搜索时,当我看到问题的标题时,我希望这个问题能帮到我,但我错了:
SwiftUI TabView with List not refreshing after objected deleted from / added to Core Data
无论如何...
在我的应用程序中,当我触摸客户项目时,详细 View 及其数据会正确显示。如果我更改任何属性并返回到第一个 View ,它会正确更新。
当我添加新位置或删除其中一个现有位置时,我的问题就开始了。当我返回第一个 View 时,消费者现有位置的数量没有更新。当我关闭并重新打开应用程序时,所有数据都会正确显示。
正如我所说,我的 Core Data 模型有 2 个实体,如下所示:
实体:客户 编号:字符串 名称:字符串 locationList:[位置](对多(位置),级联)
实体:位置 编号:字符串 地址:字符串 fromCustomer:客户(对一(客户),无效)
这是显示客户列表的 View :
struct CustomerList: View {
@Environment(\.managedObjectContext) var moc
@FetchRequest(
entity: Customer.entity(),
sortDescriptors: [
NSSortDescriptor(keyPath: \Customer.name, ascending: true),
]
) var customerList: FetchedResults<Customer>
var body: some View {
VStack{
CustomerArea
}
.navigationBarTitle(Text("Customers"), displayMode: .inline)
.navigationBarItems(leading: btnCreate, trailing: btnNew)
}
var CustomerArea: some View {
List{
ForEach(customerList, id: \.id) { customer in
NavigationLink(
destination: LocationList(selectedCustomer: customer)
) {
VStack(alignment: .leading){
Text("\(customer.name ?? "?")").font(.headline)
HStack{
Spacer()
Text("Locations: \(customer.locationList?.count ?? 0)")
.font(.footnote)
}
}.padding()
}
}.onDelete(perform: self.removeCustomer)
}
}
private func removeCustomer(at offsets: IndexSet) {
for index in offsets {
let obj = customerList[index]
self.moc.delete(obj)
}
try? self.moc.save()
}
}
最后,这是我的 LocationList View :
struct LocationList: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
@Environment(\.managedObjectContext) var moc
var requestLocations : FetchRequest<Location>
var locationList : FetchedResults<Location>{requestLocations.wrappedValue}
var selectedCustomer: Customer
init(selectedCustomer: Customer){
self.selectedCustomer = selectedCustomer
self.requestLocations = FetchRequest(
entity: Location.entity(),
sortDescriptors: [],
predicate: NSPredicate(format: "fromCustomer.id == %@", selectedCustomer.id!)
)
}
var btnNewLocation : some View { Button(action: {
let object = Location(context: self.moc)
object.id = UUID().uuidString
object.address = UUID().uuidString
object.fromCustomer = self.selectedCustomer
try? self.moc.save()
}) {
Image(systemName: "plus.circle.fill")
}
}
var body: some View {
VStack{
List{
ForEach(locationList, id: \.id) { location in
VStack(alignment: .leading){
Text("\(location.address ?? "?")").font(.headline)
.onTapGesture {
print("Tapped and changed selected parent object")
self.selectedCustomer.name = "Changed"
try? self.moc.save()
self.presentationMode.wrappedValue.dismiss()
}
}.padding()
}
.onDelete(perform: removeLocation)
}
}
.navigationBarTitle(Text("\(selectedCustomer.name ?? "?")"), displayMode: .inline)
.navigationBarItems(trailing: btnNewLocation)
}
private func removeLocation(at offsets: IndexSet) {
for index in offsets {
let obj = locationList[index]
self.moc.delete(obj)
}
try? self.moc.save()
}
}
这是第一个 View 。想象一下我触摸了最后一项:
没有位置链接到所选客户:
所以,我添加了新项目:
位置数仍然为零,但应该为 1:
最佳答案
iOS 中的一个巨大惊喜:核心数据 Controller 仅在基本字段上起作用。奇怪但真实。
令人难以置信的是,CoreData 不会根据关系进行更新。
只有基本字段(int、string 等)。
你可以看到很多关于这个奇怪问题的 QA 和文章,例如:
NSFetchedResultsController with relationship not updating
“就是这样”。
已经有很多尝试来解决这种奇怪的情况。没有真正的解决办法。 NSFetchedResultsController“就是这样”——它只适用于基本字段(字符串、整数等)。
不幸的是,情况就是这样。
注意:如果我误解了这里的问题,抱歉。但我相信这就是问题所在。
关于SwiftUI Core Data 未正确刷新 "one to many"关系且列表未刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62374472/