Mr Stanev
Mr Stanev

Reputation: 1700

Populating a tableView from a couchbase live query

I'm new to programming so please try to explain with details or examples. I'm building an app that uses couchbase-lite to display a list of results in a tableview. I want to display any changes in the list as soon as they happen so I need to use the live query and the CBLUITableSource class. I downloaded the Grocery Sync App example, but I can't quite understand how the results of the live query are displaying in the tableview. I'm also using the default master-detail template in xcode and displaying a custom cell in the tableview.

My question is how do I display the results from the live query in the table view? Do I need to use CBLUITableSource? Here's what I have so far:

My table's data soruce:

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
}

And the live query:

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

Thank you!

Upvotes: 0

Views: 706

Answers (1)

jamiltz
jamiltz

Reputation: 1144

CBLUITableSource is a convenience api to make it easier to work with live queries and table views on iOS.

Check out this repo to see how to set up a table view with CBLUITableSource.

If you need more control over the UI update when the result of the query changes you can just use the CBLLiveQuery like you did :).

Upvotes: 1

Related Questions