0
votes

I'm having a strange problem with Core Data. The problem is that I'm looking for objects with a property less than X and it isn't returning all the values that matches. There is no cache and I'm not using //[fetchRequest setFetchBatchSize:50];

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"self.level <= [cd] %@", [self.filters objectForKey:@"level"]];

I add it to a MutableArray of predicates and later I execute NSPredicate *myPredicate = [NSCompoundPredicate andPredicateWithSubpredicates: subPredicates];

This one it's in a function that returns myPredicate called preparePredicates. For the moment there aren't more predicates, only one. It's NSLog returns: level <=[cd] "30". I have tried it also with intValue of the string and %i The main function:

NSError* error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"myEntity"
                                          inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];
NSPredicate* predicate = [self preparePredicates]; 
[fetchRequest setPredicate:predicate];

//[fetchRequest setFetchBatchSize:50];
NSArray *fetchedObjects = [self.context executeFetchRequest:fetchRequest error:&error];
NSLog (@"Error: %@", error);
NSLog(@"los fetchedobjects: %i",[fetchedObjects count]);
return fetchedObjects;

It doesn't return any error. If I look at the results there isn't one having a level of 6, but there are others that matches (all are less than the specified). I know there is one with level 6. If I open the .sqlite in SQLite Database Browser, I can see it there. If I do with this program a

SELECT * FROM zmyentity WHERE zlevel <30; 

it doesn't appear, same as in Core Data do. But if I do a SELECT * FROM zmyentity WHERE zlevel == 6; it appears. I really don't know what is happening. Maybe I'm making a rookie mistake, so please point me in the right direction. Thank you very much in advance.

1

1 Answers

0
votes

Probably you have stored the level as a String attribute, because

"30" < "6"

when comparing strings. Choosing a numeric attribute type (Integer 16/32/64) should solve the problem. The corresponding level property in the myEntity class has then the NSNumber type.

You should also omit the [cd] modifier in the predicate, because that enforces a string comparison.

You predicate could then look like this:

[NSPredicate predicateWithFormat:@"self.level <= %d", [[self.filters objectForKey:@"level"] intValue]]