0

在我的 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
    }

    // ...
}
4

1 回答 1

0

如果您使用DZNEmptyDataSet 之类的东西,那么您可以在视图控制器上拥有一个状态变量并拥有不同的状态,例如 .loading、.showing、.empty、.error。对于 .showing 以外的任何状态,您将返回 0 的行数,并改为显示 DZNEmptyDataSet。因此,例如,如果您的数据无法加载,那么您将状态设置为 .error 并调用 tableView.reloadData() 来调用 emptySetDatasource 方法,您可以在其中指定错误消息。如果您有刷新控件,则用户可以拉动刷新,然后将状态恢复为 .loading 并重试。这就是由 REST 数据支持的表视图在大多数流行应用程序中的工作方式。

于 2016-10-22T07:48:24.947 回答