搜索文本在谓词 SwiftUI 中没有响应

Search Text Does not Have Response in Predicate SwiftUI

searchText 更改不会更改谓词输入,因此无论我在搜索栏中键入什么,我总是会在核心数据中获得所有提醒。

我希望我的列表根据在搜索栏中输入的文本而改变。

这是我的 SearchView

struct SearchView: View {
    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest var reminder: FetchedResults<CDReminder>
    @State var searchText: String = ""
    
    init(searchText: State<String>) {
        self._searchText = searchText
        var predicate : NSPredicate?
        if !self._searchText.wrappedValue.isEmpty{
            predicate = NSPredicate(format: "name CONTAINS %@", self._searchText.wrappedValue)
        }
        self._reminder = FetchRequest(
            entity: CDReminder.entity(),
            sortDescriptors: [],
            predicate: predicate
        )
    }
    var body: some View {
         VStack {
       SearchBar(text: searchText)
       List {
        ForEach(self._reminder.wrappedValue.filter({
            self.searchText.isEmpty ? true :
         [=10=].notes!.localizedCaseInsensitiveContains(self.searchText)
            }), id: \.self){ reminder in
            DatedReminderCell(reminder: reminder, isSelected: false, 
           onComplete: {})
            }
         }
      }
    }
}

还有我的搜索栏

    struct SearchBar: View {
    @State var text: String
    @State private var isEditing = false
    var body: some View {
            HStack {
                TextField("Search", text: $text)
                    .overlay(
                        HStack {
                            Image(systemName: "magnifyingglass")
                            if isEditing {
                                Button(action: {
                                    self.text = ""
                                }) {
                                    Image(systemName: "multiply.circle.fill")
                                }
                            }
                        }
                    )
                    .onTapGesture {
                        self.isEditing = true
                    }
            }
    }
}

我创建了另一个名为 FilteredList 的视图。在此视图中,我获取了所有提醒,然后使用谓词对其进行过滤。

 import SwiftUI
import CoreData

struct FilteredList: View {
    var fetchRequest: FetchRequest<CDReminder>
    var reminders: FetchedResults<CDReminder> {fetchRequest.wrappedValue}
    var body: some View {
        List(fetchRequest.wrappedValue , id: \.self) { reminder in
            DatedReminderCell(reminder: reminder, isSelected: false, onComplete: {})
        }
    }
    
    init(filter: String) {
        fetchRequest = FetchRequest<CDReminder>(
            entity: CDReminder.entity(),
            sortDescriptors: [],
            predicate: NSPredicate(format: "title CONTAINS %@", filter.lowercased())
        )
    }
}

然后,我用 FilteredList 更新了我的 SearchView,而不是使用 List { ForEach 语句。所以我的 SearchView 的主体现在是这样的。

var body: some View {
    VStack{
        SearchBar(text: $searchText)
            .environment(\.managedObjectContext, viewContext)
        FilteredList(filter: searchText)
    }
}

所以现在,当我在搜索栏中输入文本时,我会同时看到相关提醒。