SwiftUI FetchRequest 和 onReceive 导致无限循环

Ric*_*oon 8 loops core-data nsfetchrequest swiftui

我在 swiftUI 中有一个用于 NSManagedObject 的 @FetchRequest。当使用切换时,我试图通过调用 onReceive 来更改核心数据中第一项的标题。这,如果获取的成果在MainVC使用工作。为了证明这一点,导航按钮标题是获取的结果中有多少元素。这最终会在 ContentView onRecieve 方法中创建一个无限循环。如果导航按钮具有常规文本而不是使用 FetchedResults 中的任何内容,则没有循环,一切都按预期工作。这个循环是如何引起的,是否有更好的方法来切换特定的核心数据元素?

import SwiftUI
import CoreData

final class ContentVCModel : ObservableObject{
    @Published var newToDoItem = String()
    @Published var shouldTurnOn = false

    func createNewToDoItem(){
        let moc = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

        let toDoItem = ToDoItem(context: moc)
        toDoItem.title = self.newToDoItem
        toDoItem.createdAt = Date()
        toDoItem.cost = Cost(main: "$\(Int.random(in: 1...99))", tax: "$\(Int.random(in: 1...9))")
        toDoItem.isOn = false

        ToDoItem.save()
        self.newToDoItem = ""
    }
}

struct ContentView: View {
    @ObservedObject var model = ContentVCModel()
    @Environment(\.managedObjectContext) var managedObjectContext
    @FetchRequest(fetchRequest: ToDoItem.getAllToDoItems()) var toDoItems : FetchedResults<ToDoItem>

    var body: some View {
        List{
            Section(header : Text("Whats next?")){
                Toggle("Toggle Test", isOn: $model.shouldTurnOn)
                    .onReceive(model.$shouldTurnOn) { (newValue) in
                        self.toDoItems.first?.title = "\(Int.random(in: 23...3423))"
                        //infitine loop
                }
                HStack{
                    TextField("New Item", text: $model.newToDoItem)
                        Button(action: {
                            self.model.createNewToDoItem()
                        }){
                            Image(systemName: "plus.circle.fill")
                                .foregroundColor(self.model.newToDoItem.isEmpty ? .gray : .green)
                                .imageScale(.large)
                        }.disabled(self.model.newToDoItem.isEmpty)
                    }
                }.font(.headline)
                Section(header: Text("To Do's")){
                    ForEach(toDoItems) { toDoItem in
                        ToDoItemView(title: toDoItem.title, createdAt: toDoItem.createdAt.description, cost: toDoItem.cost, isOn: true)
                    }.onDelete { (indexSet) in
                        let deleteItem = self.toDoItems[indexSet.first!]
                        self.managedObjectContext.delete(deleteItem)
                        ToDoItem.save()
                    }
                }
            }
            .navigationBarTitle(Text("My List"))
            .navigationBarItems(trailing: EditButton())
        }
}


struct MainVC : View {
    @FetchRequest(fetchRequest: ToDoItem.getAllToDoItems()) var toDoItems : FetchedResults<ToDoItem>

    var body: some View {
        NavigationView{
            NavigationLink(destination: ContentView()) {
                Text("\(toDoItems.count)") //Using toDoItems causes this to repeat ex: "\(toDoItems.count)"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)