在我的iOS应用程序中,我有一个SQLite数据库,它有一个包含许多行的items
表。我避免将所有项目加载到内存中,而只是加载当前显示在UITableView
中的项目。在tableView(_:numberOfRowsInSection :)中抛出错误时该怎么办?
我在使用SQLite.swift,它可以在与数据库交互时使用throw
。如果从items
表获得计数throw
,那么正确的做法是什么?
我尝试显示警告,用户不能像这样关闭。
class ItemsController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var items: Items!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var count = 0
do {
count = try items.getCount();
}
catch {
// present a fatal error message
let alert = UIAlertController(
title: "Fatal Error",
message: "\(error)",
preferredStyle: .alert)
self.present(alert, animated: true, completion: nil)
}
return count
}
// ...
}
Items
类是这样的。
class Items {
var connection: Connection
func getCount() throws -> Int {
return try connection.scalar("SELECT count(*) FROM items") as! Int
}
// ...
}