【发布时间】:2017-03-04 12:08:22
【问题描述】:
在我的 iOS 应用程序中,我有一个带有 items 表的 SQLite 数据库,该表有很多行。我避免将所有项目加载到内存中,而是只加载当前在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
}
// ...
}
【问题讨论】:
标签: ios swift sqlite uitableview sqlite.swift