views:

121

answers:

1

My entity has a property (sortOrder) that is of the type Decimal(NSDecimalNumber) but when I execute a fetch request using that property as a key, I get back results in a seemingly random order. If I output the value of the property I get really strange values until I get it's intValue.

Example: The first run produces this result. The first value is the raw value of the property. The second is the intValue, the actual value of the property when I created the object - or at least I thought.

85438160 10

74691424 20

Second run...

85333744 10

85339168 20

Third...

85263696 20

85269568 10

What the hell?

Fetch Request:

NSMutableArray *cats = [[NSMutableArray alloc] initWithArray:[CoreDataHelper searchObjectsInContext:@"RestaurantMenuCategory":nil:@"sortOrder":YES:NO:nil]];

Here is the searchObjectsInContext method from my CoreDataHelperClass:

+(NSMutableArray *) searchObjectsInContext: (NSString*)entityName : (NSPredicate *)predicate : (NSString*)sortKey : (BOOL)sortAscending : (BOOL)distinct : (NSString*)distinctProperty
{
    RestaurantController *ctrl = [RestaurantController sharedRestaurantController];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:ctrl.managedObjectContext];
    [request setEntity:entity];

    if(distinct==YES){
        [request setReturnsDistinctResults:YES];
    }

    if(distinctProperty!= nil){
        [request setPropertiesToFetch :[NSArray arrayWithObjects:distinctProperty,nil]];
    }

    if(predicate != nil){
        [request setPredicate:predicate];
    }

    if(sortKey != nil){
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:sortAscending];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
       [request setSortDescriptors:sortDescriptors];
       [sortDescriptors release];
       [sortDescriptor release];
   }

   NSError *error;

   NSMutableArray *mutableFetchResults = [[[ctrl.managedObjectContext executeFetchRequest:request error:&error] mutableCopy] autorelease];

   [request release];

   return mutableFetchResults;
}
A: 

You cannot use a %d format to print out an NSDecimalNumber.

NSDecimalNumber is an Objective-C object; %d is for printing "int"s. If you use the %d format string, it will probably print something non-useful like the address of the object; use "%@" instead.

David Gelhar
Alright. Thanks for that. It doesn't solve my problem, though. I was only printing out the value to try see what was going on. I'm trying to figure out why the fetch request on the sortOrder property isn't returning them in their numeric order. It's apparently quite random at the moment.
E-Madd
David Gelhar
OK. I'll do my best to communicate it as an update to the question...
E-Madd
I can't see anything obviously wrong with what you're doing. Maybe add some logging to verify that request setSortDescriptors: is really getting called.
David Gelhar