1

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

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)会发生什么?这些语句仍然是可执行的还是简单地忽略?

4

1 回答 1

2

如果您希望即使抛出错误仍然调用您的代码,您可以为它创建一个单独的函数,并从doandcatch语句的末尾调用它。或者,您可以将该代码放在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
    })
  }
}
于 2017-05-04T03:06:35.163 回答