开发者

nspredicate with coredata fetchrequest

开发者 https://www.devze.com 2023-04-03 14:38 出处:网络
I\'m working with an app using CoreData. I have a simple structure: Name Lastname I need to update an entry so I do:

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 predicateWi开发者_开发百科thFormat:@"(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


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.


Found a way. I check the length.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号