在获取请求时返回错误?

时间:2016-12-06 23:37:38

标签: swift nsfetchrequest

我正在尝试找出如何返回我的do / catch语句的错误部分,因为它不在函数中我没有完成发送它并且它只想接受NSFetchedResultsController?

这是代码

    fileprivate lazy var fetchedExercisesTodayController: NSFetchedResultsController<UserExercise> = {

    let fetchRequest: NSFetchRequest<UserExercise> = UserExercise.fetchRequest()
    fetchRequest.predicate = NSPredicate(format: "usersroutine == %@", self.routineName)

    do {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let context = appDelegate.persistentContainer.viewContext
        let queryResults = try context.fetch(fetchRequest)

        fetchRequest.sortDescriptors = [NSSortDescriptor(key: "id", ascending: true)]
        let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.persistentContainer.viewContext, sectionNameKeyPath: nil, cacheName: nil)
        fetchedResultsController.delegate = self
        return fetchedResultsController

    } catch {
        print("Error with request: \(error)")
        return error
    }
}()

错误在&#39;返回错误&#39;行

  

无法转换类型&#39;错误&#39;的返回表达式返回类型&#39; NSFetchedResultsController&#39;

1 个答案:

答案 0 :(得分:0)

如果它不能失败,你根本不需要try / catch,所以只需删除它。

fileprivate lazy var fetchedExercisesTodayController: NSFetchedResultsController<UserExercise> = {

    let fetchRequest: NSFetchRequest<UserExercise> = UserExercise.fetchRequest()
    fetchRequest.predicate = NSPredicate(format: "usersroutine == %@", self.routineName)

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext
    let queryResults = context.fetch(fetchRequest)

    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "id", ascending: true)]
    let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.persistentContainer.viewContext, sectionNameKeyPath: nil, cacheName: nil)
    fetchedResultsController.delegate = self
    return fetchedResultsController
}() 

但是,如果你想保护失败,这可能是可能的,你可以使fetchedExcercisesTodayController成为可选变量。你必须使它成为可选项,因为如果不是,你需要返回类型为NSFetchedResultsController<UserExercise>的对象,无论如何。错误不是对象类型NSFetchedResultsController<UserExercise>。它是对象类型Error

如果你把它作为可选项,你可以返回nil并做三件事之一:

  • 调用另一种方法,如果尝试失败,则显示/执行某些操作
  • 抛出一个错误,你可以处理一些
  • 以外的地方
  • 让它为您打印错误信息

这样做的唯一问题是,如果您已经初始化并使用此变量,则必须在您使用它的每个位置开始展开该可选fetchedExercisesTodayController。从本质上讲,这两件事都可行,你只需要决定什么最适合你的代码库。

//changed to NSFetchedResultsController<UserExercise>?
fileprivate lazy var fetchedExercisesTodayController: NSFetchedResultsController<UserExercise>? = {

  let fetchRequest: NSFetchRequest<UserExercise> = UserExercise.fetchRequest()
  fetchRequest.predicate = NSPredicate(format: "usersroutine == %@", self.routineName)

  do {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext
    let queryResults = try context.fetch(fetchRequest)

    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "id", ascending: true)]
    let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.persistentContainer.viewContext, sectionNameKeyPath: nil, cacheName: nil)
    fetchedResultsController.delegate = self
    return fetchedResultsController

  } catch {
    print("Error with request: \(error)")
    //handle error in some way here
    return nil
  }
}()