SwiftUI核心数据未正确刷新“一对多”关系,并且未刷新列表

问题描述 投票:1回答:1

[我试图弄清楚我可能做错了什么(或者,当然,对关系,获取和所有东西的误解)。

在第一次搜索时,当我阅读该问题的标题时,希望该问题对我有帮助,但我错了:

SwiftUI TabView with List not refreshing after objected deleted from / added to Core Data

无论如何...

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

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

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

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

实体:位置id:字符串addess:字符串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()

    }

}


最后,这是我的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()

    }

}


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

enter image description here

没有与所选客户链接的位置:

enter image description here

所以,我添加了新项目:

So, I added on new item

位置数仍为零,但应为1:

The number of locations is still ZERO, but should be 1

swift core-data swiftui relationship one-to-many
1个回答
0
投票

iOS的一个巨大惊喜:核心数据控制器仅在基本领域起作用。奇怪但真实。

难以置信的是,CoreData 没有根据关系进行更新。

仅基本字段(整数,字符串等)。

您可以看到许多有关此奇怪问题的质量检查和文章,例如:

NSFetchedResultsController with relationship not updating

这是“就是它的样子”。

已经进行了许多尝试来解决这种奇怪的情况。没有真正的解决方案。 NSFetchedResultsController“就是它了”-它仅适用于基本字段(字符串,整数等)。

不幸的是,这种情况。

注意:如果我对这里的问题有误解,对不起。但是我相信这是问题所在。

© www.soinside.com 2019 - 2024. All rights reserved.