My list filtering is slow with the current setup:
struct TechList: View {
@FetchRequest
var devices: FetchedResults<HearingDevice>
@State private var selectedDevice: String?
@ObservedObject var model = Model()
init(predicate: NSPredicate?) {
let request: NSFetchRequest<HearingDevice> = HearingDevice.fetchRequest()
request.sortDescriptors = []
if let predicate = predicate {
request.predicate = predicate
}
_devices = FetchRequest<HearingDevice>(fetchRequest: request)
}
var body: some View {
List{
ForEach(devices, id: \.self) { device in
VStack(alignment: .center) {
HStack{
Text(device.model ?? "Unknown" + " ")
.font(.system(size: 17))
.fontWeight(.medium)
.foregroundColor(self.selectedDevice == device.model ? Color.white:Color.init(hex: "47535B"))
.multilineTextAlignment(.leading)
.padding(.leading)
Spacer()
}
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 60)
.background(self.selectedDevice == device.model ? Color.init(hex: "666699"):Color.init(hex: "F6F6F6"))
.cornerRadius(7.0)
.onTapGesture {
self.model.deviceModel = device.model!
withAnimation(.spring()){
self.selectedDevice = device.model!
}
}
}
}.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.padding(.top, 170.0)
.padding(.bottom, 23.0)
.padding(.horizontal, 10.0)
}
}
I am using an NSPredicate to filter my fetch request. When the user inputs text into a field the list then updates to reflect the input text. Filtering the list however is extremely slow, I have less than 300 records in my data set so why would this be taking so long to filter? I will also post my textfield code below to see if anyone can recognise where this slow filtering may be occurring. I would like to mention I have also tried using an @Published variable for the input predicate to track when the user input changes instead of using onEditingChanged, this did not improve performance.
TextField("Search for Device by name", text: $searchInput, onEditingChanged: {_ in
self.predicate = NSPredicate(format: "model contains %@", "\(self.searchInput)")
print("THE PREDICATE: \(String(describing: self.predicate))")
if self.searchInput == ""{
self.predicate = nil
}
})