views:

283

answers:

1

I am getting an exception when I try to get @sum on a column in iPhone Core-Data application.

My two models are following -

Task model:

@interface Task :  NSManagedObject
{

}

@property (nonatomic, retain) NSString * taskName;
@property (nonatomic, retain) NSSet* completion;

@end

@interface Task (CoreDataGeneratedAccessors)
- (void)addCompletionObject:(NSManagedObject *)value;
- (void)removeCompletionObject:(NSManagedObject *)value;
- (void)addCompletion:(NSSet *)value;
- (void)removeCompletion:(NSSet *)value;

@end

Completion model:

@interface Completion :  NSManagedObject
{
}

@property (nonatomic, retain) NSNumber * percentage;
@property (nonatomic, retain) NSDate * time;
@property (nonatomic, retain) Task * task;

@end

And here is the fetch:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"Task" inManagedObjectContext:context];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"taskName" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSError *error;
NSArray *results = [context executeFetchRequest:request error:&error];
NSArray *parents = [results valueForKeyPath:@"taskName"];
NSArray *children = [results valueForKeyPath:@"[email protected]"];
NSLog(@"%@ %@", parents, children);
[request release];
[sortDescriptor release];

The exception is thrown at the fourth line from bottom. The thrown exception is:

*** -[NSCFSet decimalValue]: unrecognized selector sent to instance 0x3b25a30
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFSet decimalValue]: unrecognized selector sent to instance 0x3b25a30'

I would very much appreciate any kind of help. Thanks.

Edit: I am on snow leopard 10.6.3 and SDK 3.1.3.

A: 

If you want to calculate the total completion percentage sum of each task you could implement a getter for "completionSum" in the task class

// interface (Task.h)
@property (nonatomic, readonly) NSNumber* completionSum;    

// implementation (Task.m)
-(NSNumber*) completionSum
{
    return [self valueForKeyPath:@"[email protected]"];
}

Calculating using @sum is slow and this solution is not KVO compliant. If you need any of this you should consider implementing a solution using KVO.

I already postet a link to an open source solution to this problem (http://qr.cx/FVi)

Martin Brugger