私のiOSアプリケーションでは、多くの行を持つitems
テーブルを持つSQLiteデータベースがあります。私はすべての項目をメモリにロードするのを避けて、現在は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
}
// ...
}