SwiftUI Core Data 未正确刷新“一对多”关系并且列表未刷新

Bon*_*ado 5 core-data one-to-many relationship swift swiftui

我试图找出我可能做错了什么(或者,当然,误解了关系、获取等等)。

在第一次搜索中,当我读到问题的标题时,我希望这个问题可以帮助我,但我错了:

SwiftUI TabView 与列表在反对从核心数据中删除/添加后不刷新

反正...

在我的应用程序中,当我触摸客户项目时,详细视图及其数据会正确显示。如果我更改任何属性并返回到第一个视图,它会正确更新。

当我添加新位置或删除现有位置之一时,我的问题就开始了。当我返回第一个视图时,消费者现有位置的数量没有更新。当我关闭并重新打开应用程序时,所有数据都会正确显示。

正如我所说,我的核心数据模型有 2 个实体,如下所示:

实体:客户 ID:字符串名称:字符串位置列表:[位置](对多(位置),级联)

实体:位置 ID:字符串地址:字符串 fromCustomer:客户(对一(客户),无效)

这是显示客户列表的视图:


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()

    }

}


Run Code Online (Sandbox Code Playgroud)

最后,这是我的 LocationList 视图:


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()

    }

}


Run Code Online (Sandbox Code Playgroud)

这是第一个观点。想象一下我触摸了最后一个项目:

在此输入图像描述

没有与所选客户关联的位置:

在此输入图像描述

所以,我添加了新项目:

所以,我添加了新项目

位置数量仍然为零,但应该为 1:

位置数量仍然为零,但应该是 1

Fat*_*tie 3

iOS 中的一个巨大惊喜:核心数据控制器仅适用于基本字段。奇怪但真实。

令人难以置信的是,CoreData并不根据关系进行更新。

仅基本字段(int、string 等)。

你可以看到很多关于这个奇怪问题的 QA 和文章,例如:

NSFetchedResultsController 的关系未更新

这就是“本来就是这样”。

人们曾多次尝试解决这种奇怪的情况。没有真正的解决方案。NSFetchedResultsController“就是这样”——它只适用于基本字段(字符串、整数等)。

不幸的是,情况就是如此。

注意:如果我误解了这里的问题,抱歉。但我相信这就是问题所在。