title: Swift - Consecutive fetch request to Core Data are unsuccessful
body: <p>I'm trying to make 3 consecutive fetch requests to core data based on the result from the previous fetch such as follow:</p>
  
  <pre><code>func myFunction(info: Info ? , completionHandler : (selectedTop: MyObject ? , selectedMiddle : MyObject ? , selectedBottom : MyObject ? , error : NSError ? ) - &gt; Void) {
      selectTop(info) {
          selectedTop,
          error in
          if let top = selectedTop {
              self.selectMiddle(top) {
                  selectedMiddle,
                  error in
                  if let middle = selectedMiddle {
                      self.selectBottom(middle) {
                          selectedBottom,
                          error in
                          if let bottom = selectedBottom {
                              completionHandler(selectedTop: top, selectedMiddle: middle, selectedBottom: bottom, error: nil)
                            } //close if bottom
                        } // close selectBottom
                    } // close if middle
                } // close selectMiddle
            } // close if top
        } // close selectTop
    } // close myFunction
  </code></pre>
  
  <p><strong>Notes</strong>: </p>
  
  <ul>
  <li>Each selectXXX function is a simple fetch request with some predicates</li>
  <li>I removed part of the code to handle errors to simplify this snippet.</li>
  </ul>
  
  <p><strong>Goal</strong>:
  Populate 3 UIImageView images (from the 3 objects) all "connected" to one another: top, middle, bottom. <em>Note that I cannot use a core data relationship (one-to-one or one-to-many) since a middle can be associated to many tops or bottoms for instance.</em></p>
  
  <p><strong>Issue</strong>:
  My top object get properly selected from Core Data but the middle does not.
  I of course verified and debugged multiple times what I have available in my core data to make sure at least one object matches all required conditions (aka fetch request predicates) but still my printing debugging shows me that no matching object is available.</p>
  
  <p><strong>Question/Doubt</strong>:
  I'm wondering if making consecutive fetch requests as such messes up with the actual data being available for the subsequent fetches. Since I'm fetching Top objects first, I'm thinking that somehow only those are available when I start fetching for Middle objects (which would explain why nothing is found)</p>
  
  <p>I tried hard looking for similar questions or issues but didn't come to anything close. Would anyone had experience with similar approach?</p>
  
  <p>Thanks in advance for the help.</p>
  
  <p>EDIT: as requested, here is the additional code from the functions involved in my fetches.</p>
  
  <pre><code>func selectTop(info: Info?, completionHandler:(result: MyObject?, error: NSError?)-&gt; Void){
      let predicateCat = NSPredicate(format: "category = %@", "Top")
      let predicateStyle = NSPredicate(format: "style IN %@", ["Style1, Style2", "Style3"])
      let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [predicateCat, predicateStyle])
      fetchDataAndSelectClothe(compoundPredicate){ success, selectedObject, error in
          if success {
              completionHandler(result: selectedObject, error: nil)
          } else {
              if let topError = error {
                  completionHandler(result: nil, error: topError)
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>func selectMiddle(selectedTop: MyObject!, completionHandler:(result: MyObject?, error: NSError?)-&gt; Void){
      let styleToUse = [selectedTop.style]
      let predicateCat = NSPredicate(format: "category = %@", "Middle")
      let predicateStyle = NSPredicate(format: "style IN %@", styleToUse)
      // other predicates are built here but removed for the sake of this example
      // ie: switch statement to select matching color based on selectedTop.color
      let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [predicateCat, predicateStyle])
  
      fetchDataAndSelectClothe(compoundPredicate){ success, selectedObject, error in
          if success {
              completionHandler(result: selectedObject, error: nil)
          } else {
              if let topError = error {
                  completionHandler(result: nil, error: topError)
          }
      }
  }
  
  
  func fetchDataAndSelectObject(compoundPredicate: NSCompoundPredicate, completionHandler:(success: Bool, selectedObject: MyObject?, error: NSError?) -&gt; Void){
      let fetchRequest = NSFetchRequest(entityName: "MyObject")
      fetchRequest.predicate = compoundPredicate
      fetchRequest.sortDescriptors = []
      let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest,
    managedObjectContext: self.sharedContext,
    sectionNameKeyPath: nil,
    cacheName: nil)
  
      do {
          try fetchedResultsController.performFetch()
      } catch let error as NSError {
          print("Error: \(error.localizedDescription)")
      abort()
      }
      // if we didn't get any data
      if fetchedResultsController.fetchedObjects?.count == 0 {
    completionHandler(success: false, selectedClothe: nil, error: NSError(domain: "FetchedResultsController", code: 0, userInfo: [NSLocalizedDescriptionKey: "There was no matching fetched Objects"]))
  }
          // if we did get data
      else {
          let numberOfItems = fetchedResultsController.fetchedObjects?.count
          let randomIndex = Int.random(0..&lt;numberOfItems!)
          let selectedObject = fetchedResultsController.fetchedObjects![randomIndex] as! MyObject
    completionHandler(success: true, selectedObject: selectedObject, error: nil)
  }
  }
  </code></pre>