Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios Core data bug with predicate

I have some strange behaviour with my Core Data.

I have entity Card, that have NSNumber property idObject

When I want to change some info in this card, I try find similar card in my CoreData, but sometimes it work, and sometimes not. I mean next:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Card"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"idObject == %@", objectId];
request.predicate = predicate;
NSError *error;
NSArray *objects = [self.backgroundManagedObjectContext executeFetchRequest:request error:&error];

Sometimes this code return objects array, but sometimes it return nil. I add next code after previous part of code:

request.predicate = nil;
NSArray *cards = [self.backgroundManagedObjectContext executeFetchRequest:request error:nil];

Error always nil, so all must be fine. And cards array has Card with idObject, that I try to find. So finally if we try execute next code several times:

NSLog(@"idObject -%@-", objectId);
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Card"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"idObject == %@", objectId];
request.predicate = predicate;
NSError *error;
NSArray *objects = [self.backgroundManagedObjectContext executeFetchRequest:request error:&error];
NSLog(@"objects count = %i", objects.count);
request.predicate = nil;
NSArray *cards = [self.backgroundManagedObjectContext executeFetchRequest:request error:nil];
NSLog(@"\nCards:");
for (Card *card in cards) {
    NSLog(@"id -%@-", card.idObject);
}

So we have next results. In first time you can see next:

2014-03-10 11:58:47.115[4397:6613] idObject -1-
2014-03-10 11:58:47.127[4397:6613] objects count = 1
2014-03-10 11:58:47.134[4397:6613] 
Cards:
2014-03-10 11:58:47.145[4397:6613] id -5-
2014-03-10 11:58:47.146[4397:6613] id -4-
2014-03-10 11:58:47.148[4397:6613] id -1-
2014-03-10 11:58:47.148[4397:6613] id -2-
2014-03-10 11:58:47.149[4397:6613] id -6-
2014-03-10 11:58:47.150[4397:6613] id -8-
2014-03-10 11:58:47.151[4397:6613] id -7-
2014-03-10 11:58:47.152[4397:6613] id -3-

In second it return me:

2014-03-10 11:59:42.752[4397:9e2b] idObject -1-
2014-03-10 11:59:42.758[4397:9e2b] objects count = 0
2014-03-10 11:59:42.761[4397:9e2b] 
Cards:
2014-03-10 11:59:42.762[4397:9e2b] id -1-
2014-03-10 11:59:42.764[4397:9e2b] id -5-
2014-03-10 11:59:42.766[4397:9e2b] id -2-
2014-03-10 11:59:42.768[4397:9e2b] id -6-
2014-03-10 11:59:42.769[4397:9e2b] id -8-
2014-03-10 11:59:42.771[4397:9e2b] id -3-
2014-03-10 11:59:42.773[4397:9e2b] id -4-
2014-03-10 11:59:42.775[4397:9e2b] id -7-

So I have in cards array always Card with such idObject, but why with my predicate, it sometimes return me that card and sometimes no? What's wrong?

like image 501
user3095583 Avatar asked Jul 11 '26 10:07

user3095583


1 Answers

In Card entity property idObject is type of NSNumber, change idObject in predicate to NSNumber ie.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"idObject == %@", [NSNumber numberWithInt:[idObject integerValue]]];

or use NSNumberFormatter to change idObject to NSNumber.

Hope this will help you

like image 66
Akhilrajtr Avatar answered Jul 15 '26 02:07

Akhilrajtr