title: How to combine 2 CoreData predicates in Swift?
body: <p>I have a function:</p>
  
  <pre><code>open func getAllCarsCount(from garages: [Garage], with categories: Set&lt;Category&gt; = []) -&gt; Int {
      var returnCount = 0
      let context = DBContext.defaultContext
  
      var predicates = [NSPredicate]()
      for category in categories {
          let predicate = NSPredicate(format: "SUBQUERY(carHasCategories, $c, $c.categoryName == %@ ).@count &gt; 0", category.categoryName)
          predicates.append(predicate)
      }
  
      for garage in garage {
          let predicate = NSPredicate(format: "garageNbr = \(garage.garageNbr) AND active = 1" )
          predicates.append(predicate)
      }
  
      let request = NSFetchRequest&lt;NSFetchRequestResult&gt;(entityName: "Car")
      request.predicate = NSCompoundPredicate(orPredicateWithSubpredicates: predicates)
  
      do {
          let count = try context.count(for: request)
          returnCount += count
      } catch let error as NSError {
          print("Could  not get all cars count. ", error.localizedDescription)
      }
  
      return returnCount
  }
  </code></pre>
  
  <p>in which I'm trying to get a number of cars which has one of the given <code>categories</code>.
  But I need to combine 2 predicates. So what I want is:</p>
  
  <p>To get ALL cars count from the given <code>garages</code> array where they are <code>active</code>, <code>garageNbr</code> are matched AND which has one of the given categories. Currently my code does not work correctly and I would really appreciate if someone could help me with it</p>
  
  <p>Database structure looks like this:</p>
  
  <p><code>Garage</code>: <code>garageNbr</code>, <code>name</code>, <code>active</code></p>
  
  <p><code>Car</code>: <code>name</code></p>
  
  <p><code>Category</code>: <code>categoryName</code></p>
  
  <p>And they have relationships:</p>
  
  <p><code>Garage</code>:
      Relationship: <code>garageHasCars</code>
      Destination: <code>Car</code>
      Inverse: <code>carInGarage</code></p>
  
  <p><code>Car</code>:
      Relationship: <code>carInGarage</code>
      Destination: <code>Garage</code>
      Inverse: <code>garageHasCars</code></p>
  
  <pre><code>Relationship: `carHasCategories`
  Destination: `Category`
  Inverse: `categoryHasCars`
  </code></pre>
  
  <p><code>Category</code>:
      Relationship: <code>categoryHasCars</code>
      Destination: <code>Car</code>
      Inverse: <code>carHasCategories</code></p>