On Feb 17, 2015, at 6:31 AM, Horatiu Potra <horati...@3pillarglobal.com> wrote:but from what I read it's not recommend to capture other external variables(local variables from within the function I set the block) in mapBlock from CBLView. Is this correct ?
Right now I can get all documents of a certain type and after that parse them until I found the item I need. Is there another way to get the item without actually parsing the entire CBLQueryEnumerator ? I know about postFilter property of CBLQuery but I was thinking if there is another way.
CBLView* view = [database viewNamed:@"cachedResponse"];
[view setMapBlock:^(NSDictionary* doc, void (^emit)(id key, id value)){
if ([doc[@"type"] isEqualToString:kCachedType]) {
emit(doc[@"_id"], doc);
}
} version: @"1"];
CBLQuery* query = [view createQuery];
query.postFilter = [NSPredicate predicateWithFormat:@"value.url = %@", url.absoluteString];
NSError *error = nil;
CBLQueryEnumerator *enumerator = [query run:&error];
if (error) {
NSLog(@"Error running query: %@", error.description);
return nil;
}
if (enumerator.count == 0) {
return nil;
}
CBLQueryRow *row = [enumerator rowAtIndex:0];
MPCachedCBLResponse *response = [MPCachedCBLResponse modelForDocument:row.document];From what I read in documentation the postFilter predicate is ran after the map block is ran, is this correct ? Do you know some other ideas on how to get that single CBLModel object that has the url search criteria ?
On Feb 17, 2015, at 11:42 PM, Horatiu Potra <horati...@3pillarglobal.com> wrote:My CBLModel object has a property string named url. I want to get one single document based on a certain url.