In my iOS app, I have a SQLite database with an items
table that has many rows. I'm avoiding loading all of the items into memory and instead only loading the ones being currently shown in the UITableView
.
I'm using SQLite.swift which can throw
when interacting with the database. If getting the count from the items
table does throw
, what's the right thing to do?
I've tried showing an alert that the user cannot close like this.
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
}
// ...
}
The Items
class is something like this.
class Items {
var connection: Connection
func getCount() throws -> Int {
return try connection.scalar("SELECT count(*) FROM items") as! Int
}
// ...
}