Performing first query on iOS

28 views
Skip to first unread message

Daniel Sonny Agliardi

unread,
Jul 10, 2015, 2:31:40 AM7/10/15
to mobile-c...@googlegroups.com
Hi, i'm working to insert couchbase in my iOS app and i am a bit stuck to this stage:
I have to get all document of a given kind and extract only the ones have a flag set true, so basically a normal query.

My actual method to do this is that:

- (NSArray *) getCompletedWorkouts{

   
NSError *error;

   
//1- createView

   
CBLView * contactInfoView = [_cl_database viewNamed: @"getCompletedWorkouts"];

   
[contactInfoView setMapBlock: MAPBLOCK({

           
if (doc[@"workout"])

                emit
(doc[@"workout"], nil);

   
}) version: @"1"];

     

   
//2 - make the query

   
CBLQuery* query = [contactInfoView createQuery];

     

   
CBLQueryEnumerator* result = [query run: &error];

   
for(CBLQueryRow* row in result)

   
{

       
NSLog(@"Found document: %@", row.document);

   
}

   
if (error) {

       
NSLog(@"Document NOT found, error = %@", error.localizedDescription);

   
}

   
return [NSArray array];

}


with this method i get in console all document, so how can i select only documents of a kind and with said flag true? In particularly i didn't found a lot of documentation on map block, is there the place where i have to place the conditions?

Sorry for my newbieness in this field, but i found couchbase a bit hard to understand at the start, more then other DB implementations. THX

Jens Alfke

unread,
Jul 10, 2015, 1:18:43 PM7/10/15
to mobile-c...@googlegroups.com
On Jul 9, 2015, at 11:04 PM, Daniel Sonny Agliardi <dan...@gmail.com> wrote:

with this method i get in console all document, so how can i select only documents of a kind and with said flag true?

The view you created will index every document that contains a property named “workout”. Querying this view with no extra parameters, as you’re doing, will return all of the documents that were indexed.

I’m not exactly sure what you want to do, but if you only want to index the documents where the “workout” property has a value of true, then change the line in the map block to
if ([doc[@"workout”] isEqual: @YES])

Or if you still want to index all docs with the “workout” property, then leave the map function alone but specify what key value you’re querying for:
query.startKey = query.endKey = @YES;

In particularly i didn't found a lot of documentation on map block, is there the place where i have to place the conditions?

Daniel Sonny Agliardi

unread,
Jul 12, 2015, 4:21:47 AM7/12/15
to mobile-c...@googlegroups.com
I was a bit confused about mapbloc... nice!

I really say thank you! This is a nice point to start! Thank you again!
Reply all
Reply to author
Forward
0 new messages