masgar
masgar

Reputation: 1873

nspredicate with coredata fetchrequest

I'm working with an app using CoreData. I have a simple structure:

Name Lastname

I need to update an entry so I do:

if (strLastname.lengt > 0) {
  predicate = [NSPredicate predicateWithFormat:@"(dbName LIKE[c] %@ AND dbLastname LIKE[c] %@)", strName, strLastname];
else {
  predicate = [NSPredicate predicateWithFormat:@"(dbName LIKE[c] %@)", strName];
}

now the problem is when my DB contains more user with same name and one of them has no lastname, the predicate will match always the first entry where the name is like strName so I should check if dbLastname is like '' but it fails:

if (strLastname.lengt > 0) {
  predicate = [NSPredicate predicateWithFormat:@"(dbName LIKE[c] %@ AND dbLastname LIKE[c] %@)", strName, strLastname];
else {
  predicate = [NSPredicate predicateWithFormat:@"(dbName LIKE[c] %@ AND dbLastname LIKE[c] '')", strName];
}

the predicate is OK but fetchedObject is empty.

Any help is appreciated.

Max

Upvotes: 0

Views: 505

Answers (2)

masgar
masgar

Reputation: 1873

Found a way. I check the length.

[NSPredicate predicateWithFormat:@"(dbName LIKE[c] %@ AND not dbLastname.length > 0)", strName];

Upvotes: 0

ophychius
ophychius

Reputation: 2653

Instead of LIKE try CONTAINS, this fixed the issue for me when I ran into the same problem. Also, check all the spelling properly, the predicate is correct no matter what field names and variables you stick in there.

Upvotes: 0

Related Questions