从 couchbase 实时查询填充 tableView

Populating a tableView from a couchbase live query

我是编程新手,所以请尝试用细节或例子来解释。我正在构建一个应用程序,它使用 couchbase-lite 在 table 视图中显示结果列表。我想在列表发生任何变化时立即显示它们,因此我需要使用实时查询和 CBLUITableSource class。我下载了 Grocery Sync App 示例,但我不太明白实时查询的结果如何显示在 table 视图中。我还在 xcode 中使用默认的主从模板,并在 table 视图中显示自定义单元格。

我的问题是如何在 table 视图中显示实时查询的结果?我需要使用 CBLUITableSource 吗?这是我目前所拥有的:

我的table的数据来源:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: MatchCellTableViewCell = tableView.dequeueReusableCellWithIdentifier("matchcell", forIndexPath: indexPath) as! MatchCellTableViewCell

    return cell
}

实时查询:

func initializeQuery() {
    let query = database.viewNamed("matches").createQuery()
    liveQuery = query.asLiveQuery()
    liveQuery.addObserver(self, forKeyPath: "rows", options: nil, context: nil)
    liveQuery.start()
}

谢谢!

CBLUITableSource 是一种便利 api,可以更轻松地处理 iOS.

上的实时查询和 table 视图

查看 this repo 了解如何使用 CBLUITableSource 设置 table 视图。

如果您需要在查询结果更改时更好地控制 UI 更新,您可以像以前一样使用 CBLLiveQuery :).