Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Use a CouchDB view to fetch the list of items

Instead of getting _all_docs, use a view that sorts items by date.
This fixes a bug where design docs in the database would show up as empty items in the list.
It also removes the necessity of constructing document IDs that sort by date; I've left the code in for now because it doesn't cause any problems.
NOTE: There are still some sort problems because some platform isn't writing dates in the correct format; entries created by that will stick to the top until deleted.
  • Loading branch information...
1 parent 3987fff commit 043eb799c7d281ac3a49c22d6ae1b10445316f2c @snej snej committed
Showing with 8 additions and 2 deletions.
  1. +8 −2 Source/RootViewController.m
10 Source/RootViewController.m
@@ -89,8 +89,14 @@ - (void)viewWillAppear:(BOOL)animated {
- (void)useDatabase:(CouchDatabase*)theDatabase {
self.database = theDatabase;
- CouchLiveQuery* query = [[database getAllDocuments] asLiveQuery];
- query.descending = YES; // Sort by descending ID, which will imply descending create time
+ // Create a CouchDB 'view' containing list items sorted by date:
+ CouchDesignDocument* design = [theDatabase designDocumentWithName: @"grocery"];
+ [design defineViewNamed: @"byDate"
+ map: @"function(doc) {if (doc.created_at) emit(doc.created_at, doc);}"];
+ CouchLiveQuery* query = [[design queryViewNamed: @"byDate"] asLiveQuery];
+ query.descending = YES; // Sort by descending date, i.e. newest items first
self.dataSource.query = query;
self.dataSource.labelProperty = @"text"; // Document property to display in the cell label
[self updateSyncURL];

0 comments on commit 043eb79

Please sign in to comment.
Something went wrong with that request. Please try again.