Hi, I want to search from both candies and countries. But, it searches only from countries, and shows all the candies related to country.
1.How to make that search from candies works too?
Edit: Somehow working, but there's another Issue. When picker choise is "candy", origin country reprints each time
It shows:
UNITED KINGDOM
Mars
UNITED KINGDOM
KitKat
Instead of
UNITED KINGDOM
Mars
KitKat
2.How to understand that there's no match for request? so I can display "No matching results."
// this is what I've tried
@FetchRequest(entity: [Country.entity(), Candy.entity()], sortDescriptors: []) var countries: FetchedResults<Country, Candy>
init(filterKey: String,
filterValue: String,
strategy: FilterStrategy = .beginsWith,
sortDescriptors: [NSSortDescriptor],
@ViewBuilder content: @escaping (SomeCoreDataObject) -> Content) {
var predicate = ""
switch strategy {
case .beginsWith:
predicate = "BEGINSWITH"
case .contains:
predicate = "CONTAINS"
case .endsWith:
predicate = "ENDSWITH"
}
// Create and run the fetch request
fetchRequest = FetchRequest<SomeCoreDataObject>(entity: SomeCoreDataObject.entity(),
sortDescriptors: sortDescriptors,
predicate: NSPredicate(format: "%K \(predicate)[c] %@", filterKey, filterValue))
self.content = content
}
struct ContentView: View {
@Environment(\.managedObjectContext) var moc
@FetchRequest(entity: Country.entity(), sortDescriptors: []) var countries: FetchedResults<Country>
private let keys = [Key(userFacingName: "country", internalName: "fullName"),
Key(userFacingName: "candy", internalName: "name")]
@State private var selectedKey = 0
struct Key {
var userFacingName: String
var internalName: String
}
private let strategies = ["begins with", "contains", "ends with"]
@State private var selectedStrategy = 0
@State private var letters = ""
var body: some View {
VStack {
if letters.isEmpty {
List {
ForEach(countries, id: \.self) { country in
Section(header: Text(country.wrappedFullName)) {
ForEach(country.candyArray, id: \.self) { candy in
Text(candy.wrappedName)
}
}
}
}
}
else if selectedKey == 1 {
FilteredList(filterKey: keys[selectedKey].internalName,
filterValue: letters,
strategy: FilterStrategy(rawValue: selectedStrategy)!,
sortDescriptors: []) { (candy: Candy) in
Section(header: Text(candy.originCountryName)) {
Text(candy.wrappedName)
}
}
} else if selectedKey == 0 {
FilteredList(filterKey: keys[selectedKey].internalName,
filterValue: letters,
strategy: FilterStrategy(rawValue: selectedStrategy)!,
sortDescriptors: []) { (country: Country) in
Section(header: Text(country.wrappedFullName)) {
ForEach(country.candyArray, id: \.self) { candy in
Text(candy.wrappedName)
}
}
}
}
Form {
Section(header: Text("Filter on Key")) {
// How to filter?
Picker("Filter Strategy", selection: $selectedKey) {
ForEach(0..<keys.count) {
Text(keys[$0].userFacingName)
}
}
.pickerStyle(SegmentedPickerStyle())
}
Section(header: Text("Filter Strategy")) {
// How to filter?
Picker("Filter Strategy", selection: $selectedStrategy) {
ForEach(0..<strategies.count) {
Text(strategies[$0])
}
}
.pickerStyle(SegmentedPickerStyle())
}
Section() {
TextField("Search", text: $letters)
}
}