0

I have a Core Data model of cars created and I'd like select one of vehicle type PLUS any of several different colors Here's what I envision to do this:

Selection ViewController

1) Select the type of car (sedan, coupe, SUV, hybrid) (I envision a segmented control to set this as a predicate)

2) Once the type of vehicle is selected, I'd like to select from a list of colors and display only cars that satisfy the type of vehicle I selected in the first step. For example, I'd like to display only SUVs that are (red, green, and blue) or only hybrids that are black and white.

Results TableViewController

3) Once those are selected, I'd like to dump them on another TVC.

This post helps a bit, but person asking the question trying to find disparate items (ex: A, B, & C). I'm trying to find X, Y & Z within only A. Can I apply multiple predicates to an NSFetchRequest? Would it be better to manually parse my results?

I'm running into trouble when I try retrieve one type of vehicle plus a list of selected colors

let vehiclePredicate = NSPredicate(format: "type == %@", "(one of sedan/coupe/SUV/hybrid)")

AND any of the following criteria

let colorRedPredicate = NSPredicate(format: "color == %@", "red")
let colorGreenPredicate = NSPredicate(format: "color == %@", "green")
let colorBluePredicate = NSPredicate(format: "color == %@", "red")
let colorBlackPredicate = NSPredicate(format: "color == %@", "black")
let colorWhitePredicate = NSPredicate(format: "color == %@", "white")

let colorPredicates: NSArray = [**add colors here when they're selected**]

I'm trying to figure out the cleanest way to do this, but the examples I've found only retrieve disparate items.

The other roadblock I've run into is how to get the results from my SelectionVC to the ResultsTVC. I know I'm going to need a delegate, but what will I need? Any assistance on this would be greatly appreciated.

Community
  • 1
  • 1
iOSPadawan
  • 146
  • 2
  • 10

2 Answers2

1

You want to end up with a predicate like:

type == %@ AND color IN %@

And then supply your type as a string and the colours as a collection. You can do this with a compound predicate created from 2 others or just change what your UI selection means and generate the two parameters as the segment name and an array of colours.

If you want to combine multiple options of the oboe then you need to create each and then use a compound predicate to combine them.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Thank you! Using what I've got above, what would the complete predicate look like for those of us who are hanging on by the skin of their teeth? – iOSPadawan Jan 06 '15 at 20:08
  • Well, I don't get how your sedan AND red AND blue would work, I expect you want to OR the colours, and then the format I show is the only one you need, together with a compound predicate if you want multiple type + colour choices. – Wain Jan 06 '15 at 20:10
1

Your question is not clear. Your predicates have wrong syntax - are unintelligible. That being said, I think I guess what you want, so here it is.

You could start with a fully loaded table view without any filters. You could implement a filter menu (or buttons) and present the appropriate control (segmented control, table view with multiple selection, table view with single selection, slider etc) modally.

In your original table view you use a NSFetchedResultsController, and you keep a NSPredicate as a property. Whenever the predicate changes, you nil out the fetched results controller and reload your table.

The modal control views can feed back to your master view controller which then modifies the predicate. You can combine all predicates with the NSCompoundPredicate API. Maybe it is most convenient to keep them in a mutable array, so you have them handy.

var predicateArray = [NSPredicate]()

E.g. start with this

predicate = NSPredicate(value: "true")
predicateArray.append(predicate)

Now the color controller sends back ["red", "green", "blue"]. You could construct or predicates by constructing the right predicate string, or by using orPredicateWithSubpredicates, but it might be easier to just use collection operators:

let colorPredicate = NSPredicate(format: "color in %@", colorStrings)!
predicateArray.append(colorPredicate)

In your fetched results controller getter you reconstruct the predicate based on your predicate array:

fetchRequest.predicate = 
   NSCompoundPredicate.andPredicateWithSubpredicates(predicateArray)
Mundi
  • 79,884
  • 17
  • 117
  • 140