SwiftUI FetchRequest和onReceive导致无限循环

时间:2019-10-06 02:02:26

标签: loops core-data swiftui nsfetchrequest

我在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)"
            }
        }
    }
}

0 个答案:

没有答案