iphone - Core Data Query Multiple Columns with Single Search String -
i have simple core data model 2 string attributes (size , category). given search string 'small widget' possible return records match query words @ least 1 attribute (i.e. records 'small' size , 'widget' category)? have:
nsstring *search = @"small widget" nspredicate *predicate = [nspredicate predicatewithformat:@"size contains[cd] %@ or category contains[cd] %@", search, search]; ...
this won't return results (as no category or size equals "small widget"). suggestions? note search strings user entered text field , may come in order can't split manually.
i haven't tested looks want this:
nsstring *search = @"small widget"; nspredicate *predicate = [nspredicate predicatewithformat:@"%@ contains[cd] size , %@ contains[cd] category", search, search];
the search string contain (or not) size , category, should ask if current size or category contained on search string.
you split search string , modify predicate. should them identify 1 performs better
nsstring *search = @"small widget"; nsarray *array = [search componentsseparatedbystring:@" "]; nsmutablearray *subpredicates = [nsmutablearray array]; (nsstring *q in array) { [subpredicates addobject: [nspredicate predicatewithformat:@"size contains[cd] %@ or category contains[cd] %@", q, q]]; } nscompoundpredicate *predicate = [[[nscompoundpredicate alloc] initwithtype:nsandpredicatetype subpredicates:subpredicates] autorelease];
Comments
Post a Comment