在我的 iOS 应用程序中,我有一个 SQLite 数据库,其中包含一个items
包含许多行的表。我避免将所有项目加载到内存中,而是只加载当前显示在UITableView
.
我正在使用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
}
// ...
}