Problems with CBLView

15 views
Skip to first unread message

Endika Montejo

unread,
Jul 29, 2014, 12:06:35 PM7/29/14
to mobile-c...@googlegroups.com
Good afternoon,

I am having some problems when trying to make a general find function


The inputs for this function are the repository (type of document), indexName (property to emit as a key), indexMinValue (key's min value), indexMaxValue (key's max value), limit and descending.

So my function looks like this:

- (NSMutableArray*) findGroup: (NSString *) repository
                    indexName
: (NSString *) indexName
                indexMinValue
: (id) indexMinValue
                indexMaxValue
: (id) indexMaxValue
                        limit
: (int) limit

                   
descending: (BOOL) descending{

   
CBLView* findGroupView = [[self.couchDBProvider getLocalDB] viewNamed:@"findGroupView"];
   
[findGroupView setMapBlock: MAPBLOCK({
       
NSString* type= [doc valueForKey:@"type"];
       
if([type isEqualToString:repository]){
            id orderKey
= [doc valueForKey: indexName];
           
if(orderKey){
                emit
(orderKey, doc);
           
}
       
}
   
}) version: @"1.0"];

   
CBLQuery* findGroupQuery=findGroupView.createQuery;


And later I set the properties of findGroupQuery.


But I have problems as findGroupView doesn't always get as orderKey the value of the property given in the function (indexName). 

Sometimes orderKey gets the value of other previously sent indexName variable.


Any help? 

Jens Alfke

unread,
Jul 29, 2014, 12:52:01 PM7/29/14
to mobile-c...@googlegroups.com
That’s not a valid map function. It uses external state, the variables “repository” and “indexName”. A map function has to be a pure function that always emits the same values given the same input document, otherwise the index won’t be consistent.

Another way to think of it is that the map function is for indexing the database, not for querying it. Mixing query values into the map function is wrong.

—Jens
Reply all
Reply to author
Forward
0 new messages