Coredata fetch request using NSPredicate - core-data

I want to do a phone number search in sqlite db. Phone table have a number field which holds a string value (eg: +1 (2-34)56-78 ).
If user enters search text as "234", then I need to fetch the phone numbers with only digits(eliminating all other alphabets or special characters in the string stored) using NSPredicate and compare it with the entered string. Should I do this using a predicate in a fetch request?
I tried with following predicate and it doesn't return me the expected result:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"contactName != null AND SUBQUERY(phoneList, $phone, ANY $phone.number CONTAINS[c] %#).#count > 0",searchText];
IS there any way to get this done? Can I use regular expression with NSPredicate for this?

NSPredicate doesn't support that kind of matching. Your Core Data model should include data that your app requires for filtering-- which might mean extra fields that are not normally visible to users. If you need to match numbers without regard to non-numeric characters, you should add a field which contains that data. If the actual string is +1 (2-34)56-78, add a secondary field where you store 12345678. Use this extra field in the predicate.

Related

Filter CoreData entity with array of strings

I have a problem. User enters a text on a UITextField, for example: "word sentence". Now, when I filter CoreData entity I do this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"title CONTAINS[cd] %# ", text];
But results returned contains entire text "word sentence", but I want do a search for every word. Is it possible with CoreData?
As written in my comment you could split the predicate with OR (through a compound predicate). For example.
[title CONTAINS[cd] %# OR title CONTAINS[cd] %#, text1, text2]
where text1 is the first term and text2 is the second. This predicate could be also created dynamically. I mean, based on text the user inserts.
If you use cd the research could be affected (perfomance will be better without it).

How do I filter CoreData results based on a relationship

I have a list of users, each with
Users.accounts--->Accounts.measurements--->measurement.account
I want to pull all measurements from the measurements "table" where measurement.account=a specific account, and where measurement.type (a property) is "wind speed" as an example.
So I:
Create a fetch request on measurements
I want to create a predicate that says type="wind speed" and account={NSAccount object}
How do I do this?
Measurement type is easy:
*predicate = [NSPredicate predicateWithFormat:#"measuretype = 'air speed'];
Specifying the relationship, I don't know:
myAccountObject *myAccount=[accountFromId:#"6785"];
*accountpredicate = [NSPredicate predicateWithFormat:#"account = ???",myAccount];
In a predicate, %# is a var arg substitution for an object value, so
[NSPredicate predicateWithFormat:#"account = %#",myAccount]
is probably what you are looking for. Even for a constant string this is preferable, because it avoids problems with embedded quotation marks or other special characters, which would
have to be quoted otherwise. So your combined predicate would be:
[NSPredicate predicateWithFormat:#"measuretype = %# AND account = %#",
#"air speed", myAccount]
For more information, see "Predicate Format String Syntax" in the
"Predicate Programming Guide".

What is the correct way for quering a NSNumber in a NSArray within CoreData?

I have a Entity with a column of type ID named "responsibleUsers". In this column I store an Array containing NSNumbers.
I want to fetch all objects of this entity, that match my current User. Therefore i create following predicate:
[NSPredicate predicateWithFormat: #"%# IN responsibleUsers",[NSNumber numberWithInteger: curUser.oID.integerValue] ]
whatever I try, my App crashes. Once with a EXC_BAD_ACESS, once with "unimplemented SQL generation for predicate nsnumber"
What is the correct way to query my entity?
The query you are trying assumes that you have two entities: the entity you querying (let's call it Group) and another one, perhaps called User, which is set up as a to-many relationship from Group called responsibleUsers. You would then be able to use the predicate you suggest:
[NSPredicate predicateWithFormat:#"%# IN responsibleUsers, aUser];
This would be the recommended use of Core Data object graphs.
In your setup, it seems you have an array of NSNumber set as a property rather than a relationship. Therefore you cannot use such a query. You simply have to retrieve the array and query the array.
BOOL containsResponsibleUser = NO;
for (NSNumber *n in aGroup.responsibleUsers) {
if ([n isEqualTo:[NSNumber numberWithInteger: curUser.oID.integerValue]])
containsResponsibleUser = YES;
}
If you are indeed querying something like a group of users, I would recommend the first approach. If you are querying some kind of user, I would suggest a BOOL property responsible as the most efficient solution.

Core Data NSPredicate casting key value

I have a data model which has values of type id that I was planning on casting appropriately when needed.
Is it possible for me to cast these as strings and compare them to strings from a UISearchBar using NSPredicate or do I have to use another method?
Maybe something like this:
NSPredicate * predicate;
predicate = [NSPredicate predicateWithFormat:#"CAST(%K) contains[cd] %#", employeeID , theSearchBar.text];
No. The CAST() function doesn't work that way. I think you just have to assume that the id returned from -employeeID is comparable to a string.

having problem with NSPredicate predicateWithFormat "OR"

I am trying to fetch an entity from coredata using NSPredicate. I am trying to do a 'search as you type' approach. It works fine on a simple product name like "chair" or "Table".
But as soon as I tried the more complicate stuff like "Wheelchair - electric - power" then when I type in "Wheelchair electric" the result is not showing. I think it has to do with my NSPredicate predicateWithFormat but I've been at this for awhile now. So I need some help.
Here is my code (these are my best guesses)
request.predicate = [NSPredicate predicateWithFormat:#" Name BEGINSWITH [c] %# ",searchText];
request.predicate = [NSPredicate predicateWithFormat:#" Name BEGINSWITH [c] %# OR Name like[cd] %# ",searchText,searchText];
Thanks in advance :)
Pondd
To have an OR search you will have to first split the search term by whitespace into an array. Then you need to create a LIKE predicate for each individual component. Finally you have to combine the individual predicates with several OR compound predicates.

Resources