Group By Query for Sectioned UITableView with UITableSource

Using CBLite 1.4.1:

I am having trouble understanding if you can do a group by query in CBLite. I see the CBLQuery.groupLevel, however, that does not make it clear how to have a multi-sectioned table view using UITableSource.

I have a document of type “Inspection”. Inspections have a status (String). I need to group the document query based on the value of the “status” key. How do I use “groupLevel” property to this? Or is there a better way that I am missing? I do not need to aggregate documents into rows. I need to group the rows into sections for a display in a sectioned table view.



A couple of related ways to do this, I think.

You could create a View that only emits for docs of type “Inspection” with the status as the key.
Or you could emit an array of [ “Inspection”, status] or similar.

By default Query produces results ordered by ascending values of the key, so that would produce the groupings for you.

Grouplevel has to do with the grouping for reducing. Since you don’t want aggregation, it’s not what you want.

That’s not what grouping in a query does. It aggregates multiple rows into a single row. If you grouped docs of type “Inspection” by the value of “status”, you’d get one row for each status, optionally containing aggregate data like the average price or the maximum days overdue or something.

It sounds like you want to sort by status, and then collect the rows into sections of the table. To do that, just use status as the first element of the emitted key, and put some other element after it to act as the secondary sort (e.g. start date or inspection ID). Then as you iterate the results, just watch for the status to change and start a new group there.