Below in my code I have simple setup whereby my FetchRequest is filtered by a string, this string comes from a text field that the user can input into. How do I return all results if said text field is empty?
struct TechList: View {
var fetchRequest: FetchRequest<HearingDevice>
@State private var selectedDevice: String?
@ObservedObject var model = Model()
init(filter: String) {
fetchRequest = FetchRequest<HearingDevice>(entity: HearingDevice.entity(), sortDescriptors: [], predicate: NSPredicate(format: "model CONTAINS %@", filter))
}
var body: some View {
VStack(alignment: .center){
List(fetchRequest.wrappedValue, 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, 160.0)
.padding(.bottom, 20.0)
}
}
}
Is there a way I can return ALL of my results if there is no filter provided?
Update based on Asperi's comment:
struct TechList: View {
var filter: String
@FetchRequest(fetchRequest: HearingDevice.fetchRequest()) var fetchRequest: FetchedResults<HearingDevice>
@State private var selectedDevice: String?
@ObservedObject var model = Model()
init(filter: String) {
self.filter = filter
_fetchRequest = FetchRequest<HearingDevice>(entity: HearingDevice.entity(), sortDescriptors: [], predicate: NSPredicate(format: "model CONTAINS %@", filter))
}
var body: some View {
VStack(alignment: .center){
List(fetchRequest, 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, 160.0)
.padding(.bottom, 20.0)
}
}
}
I have implemented the approach from your first comment although there are no errors and the code executes fine, my list remains empty. How do I pull the fetch request into my list if I am not using .wrappedValue?
MainView + filterString&ResultViewwith a reconstructed fetch request ininit(as you currently do) depending on filterString content with or w/o predicate.