Swift - 对Core Data的连续获取请求不成功

时间:2015-11-30 01:55:05

标签: ios swift core-data nsfetchrequest

我试图根据之前提取的结果对核心数据进行3次连续提取请求,如下所示:

func myFunction(info: Info ? , completionHandler : (selectedTop: MyObject ? , selectedMiddle : MyObject ? , selectedBottom : MyObject ? , error : NSError ? ) - > 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

备注

  • 每个selectXXX函数都是带有一些谓词的简单获取请求
  • 我删除了部分代码以处理错误以简化此代码段。

目标: 所有"连接"填充3个UIImageView图像(来自3个对象)彼此:顶部,中部,底部。 请注意,我不能使用核心数据关系(一对一或一对多),因为中间可以与许多顶部或底部相关联。

问题: 我的顶级对象从Core Data中正确选择,但中间没有。 我当然验证并调试了我在核心数据中可用的多次,以确保至少有一个对象符合所有必需条件(也就是获取请求谓词)但仍然我的打印调试显示我没有匹配的对象可用。

问题/怀疑: 我想知道连续的提取请求是否会因为后续提取的实际数据而变得混乱。由于我首先获取Top对象,所以我认为只有当我开始获取Middle对象时才能获得这些对象(这可以解释为什么没有找到)

我努力寻找类似的问题或问题,但没有结束。有人会有类似方法的经验吗?

提前感谢您的帮助。

编辑:根据要求,这是我的提取中涉及的功能的附加代码。

func selectTop(info: Info?, completionHandler:(result: MyObject?, error: NSError?)-> 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)
        }
    }
}

}

func selectMiddle(selectedTop: MyObject!, completionHandler:(result: MyObject?, error: NSError?)-> 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?) -> 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..<numberOfItems!)
        let selectedObject = fetchedResultsController.fetchedObjects![randomIndex] as! MyObject
  completionHandler(success: true, selectedObject: selectedObject, error: nil)
}
}

0 个答案:

没有答案