【问题标题】:The do statement [closed]do 语句 [关闭]
【发布时间】:2017-10-02 01:16:33
【问题描述】:

假设这是我们从模型中删除对象的函数:

func delete(indexPath: IndexPath) {

    let managedObject = self.fetchedResultsController.object(at: indexPath)
    self.managedObjectContext.delete(managedObject)

    do {

        // Save changes
        try self.managedObjectContext.save()

        // Cancel the notification
        let center = UNUserNotificationCenter.current()
        center.removeDeliveredNotifications(withIdentifiers: [managedObject.uuid!])
        center.removePendingNotificationRequests(withIdentifiers: [managedObject.uuid!])

        // Reload tableView
        self.tableView.reloadData()

    } catch {

        let alertController = UIAlertController.init(title: "Error", message: "We are sorry! Unknown error occured...", preferredStyle: .alert)
        alertController.addAction(UIAlertAction.init(title: "Close", style: .default, handler: { (action) in
            // Completion handler
        }))
        self.present(alertController, animated: true, completion: {
            // Completion block
        })
    }
}

问题:

如果抛出错误,执行转移到catch 子句。那很好。但是语句(其余代码,取消通知和重新加载 tableView)会发生什么?这些语句仍然是可执行的还是简单地被忽略?

【问题讨论】:

  • 一旦在这行try self.managedObjectContext.save() 中抛出错误,其余代码将不会运行,它将进入catch 语句。即使抛出错误,您还想运行语句吗?
  • @ShamasS 不!抛出错误时我不需要任何语句。感谢您的评论
  • 这些行将被忽略,因为上一行有错误。如果这就是您想要的并且正在发生的事情,您能否重新表述您的问题?
  • @ShamasS 这是一个实验。现在我知道 do-catch 语句的工作原理了:)

标签: swift core-data usernotifications do-catch


【解决方案1】:

如果您希望即使抛出错误也仍然调用您的代码,您可以为它创建一个单独的函数,并从docatch 语句的末尾调用它。或者您可以将该代码放在defer 语句中。

类似

func delete(indexPath: IndexPath) {

let managedObject = self.fetchedResultsController.object(at: indexPath)
self.managedObjectContext.delete(managedObject)

do {

    // Save changes
    try self.managedObjectContext.save()
    reloadCode()

} catch {
    reloadCode()
    let alertController = UIAlertController.init(title: "Error", message: "We are sorry! Unknown error occured...", preferredStyle: .alert)
    alertController.addAction(UIAlertAction.init(title: "Close", style: .default, handler: { (action) in
        // Completion handler
    }))
    self.present(alertController, animated: true, completion: {
        // Completion block
    })
}
}

func reloadCode() {
        // Cancel the notification
        let center = UNUserNotificationCenter.current()
        center.removeDeliveredNotifications(withIdentifiers: [managedObject.uuid!])
        center.removePendingNotificationRequests(withIdentifiers: [managedObject.uuid!])

        // Reload tableView
        self.tableView.reloadData()

}

或者更简洁,

func delete(indexPath: IndexPath) {

let managedObject = self.fetchedResultsController.object(at: indexPath)
self.managedObjectContext.delete(managedObject)


  defer {
 let center = UNUserNotificationCenter.current()
        center.removeDeliveredNotifications(withIdentifiers: [managedObject.uuid!])
        center.removePendingNotificationRequests(withIdentifiers: [managedObject.uuid!])

        // Reload tableView
        self.tableView.reloadData()
}
do {

    // Save changes
    try self.managedObjectContext.save()

    // Cancel the notification


} catch {

    let alertController = UIAlertController.init(title: "Error", message: "We are sorry! Unknown error occured...", preferredStyle: .alert)
    alertController.addAction(UIAlertAction.init(title: "Close", style: .default, handler: { (action) in
        // Completion handler
    }))
    self.present(alertController, animated: true, completion: {
        // Completion block
    })
  }
}

【讨论】:

    猜你喜欢
    • 2020-03-08
    • 1970-01-01
    • 2015-02-06
    • 2016-01-10
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    相关资源
    最近更新 更多