私の 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
}
// ...
}