Ok, so basically i have a core data database with customer information in. I have a searchbar also. I want it so i can select whether to search name, address, etc.etc.etc.
Here is the code i have at the moment:
[sosdRequest setPredicate:[NSPredicate predicateWithFormat:@"(AccountCode contains[cd] %@
AND %d>0) AND (CompanyName contains[cd] %@ AND %d>0) AND (Address1 contains[cd] %@ AND %d>0)
AND (Address2 contains[cd] %@ AND %d>0) AND (Town contains[cd] %@ AND %d>0) AND (County
contains[cd] %@ AND %d>0) AND (Postcode contains[cd] %@ AND %d>0) AND (UserField01
contains[cd] %@ AND %d>0) AND (UserField02 contains[cd] %@ AND %d>0) AND (UserField03
contains[cd] %@ AND %d>0) AND (UserField04 contains[cd] %@ AND %d>0) AND (UserField05
contains[cd] %@ AND %d>0)",searchBar.text, _searchAccountCodeOnly, searchBar.text,
_searchNameOnly, searchBar.text, _searchAddress, searchBar.text, _searchAddress,
searchBar.text, _searchTown, searchBar.text, _searchCounty, searchBar.text, _searchAddress,
searchBar.text, _searchUser1, searchBar.text, _searchUser2, searchBar.text, _searchUser3,
searchBar.text, _searchUser4, searchBar.text, _searchUser5]];
So with this, ‘_searchAddress’ ‘_searchNameOnly’ etc.etc. are integers. If i have say
_searchAccountCodeOnly = 0;
_searchNameOnly = 0;
_searchNumbers = 0;
_searchTown = 0;
_searchCounty = 0;
_searchAddress = 2;
_searchContact = 0;
_searchUser1 = 0;
_searchUser2 = 0;
_searchUser3 = 0;
_searchUser4 = 0;
_searchUser5 = 0;
my search will still return based on CompanyName. This is driving me insane and i’m about to run on my lunchbreak to clear my head a tad but if anyone has any ideas it would be greatly appreciated 🙂
I don’t know if that actually works (and how effective Core Data/sqlite is with optimizing such a query), but to ignore one attribute in the query you probably mean
so that the subexpression evaluates to TRUE if the corresponding flag is zero.
A better way would be to build an array of predicates with the queries that you actually need and then use