• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Calculating the max value for a core data attribute
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Calculating the max value for a core data attribute


  • Subject: Calculating the max value for a core data attribute
  • From: Darren Wheatley <email@hidden>
  • Date: Fri, 18 Dec 2009 00:56:22 +0000

Hi,

I need to find out the maximum value of an attribute of a core data entity. I'm still in the Cocoa learning curve, and this is a simple test app that I'm using to learn.

The app imports fortunes from a text file and displays a table on the screen. The imports are done in a separate, background thread.

I found this code online, which I have attempted to get working:

- (double)getMaxID
{
    NSLog(@"in getMaxID");  // debug

// Use a new moc with the original persistentStoreCoordinator to ensure thread safety
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init];
[moc setPersistentStoreCoordinator:[[self delegate] persistentStoreCoordinator]];


// Create fetch
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:@"Fortune" inManagedObjectContext:moc]];
[fetch setResultType:NSDictionaryResultType];


// Expression for Max ID
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"id"];
NSExpression *minExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:@"maxID"];
[expressionDescription setExpression:minExpression];
[expressionDescription setExpressionResultType:NSDoubleAttributeType];
[fetch setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];


    // Execute the fetch.
    double theID = 0;
    NSError *error = nil;
    NSArray *objects = nil;
    objects = [moc executeFetchRequest:fetch error:&error];

if (objects && ([objects count] > 0))
{
NSLog(@"query successful"); // debug
theID = [((NSNumber *)[[objects objectAtIndex:0] valueForKey:@"maxID"]) doubleValue];
}
else
{
NSLog(@"Setting default value for theID"); // debug
theID = 0;
}


    return(theID);
}


My entity is called "Fortune" and the attribute is called "id" (a Double).

When the code runs it crashes when the fetch request is executed. The console shows this:

2009-12-18 00:53:42.777 FortunesHelperMVC[4027:1703] -[NSCFNumber count]: unrecognized selector sent to instance 0x1004d7b10
2009-12-18 00:53:42.778 FortunesHelperMVC[4027:1703] An uncaught exception was raised
2009-12-18 00:53:42.778 FortunesHelperMVC[4027:1703] -[NSCFNumber count]: unrecognized selector sent to instance 0x1004d7b10
2009-12-18 00:53:42.779 FortunesHelperMVC[4027:1703] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFNumber count]: unrecognized selector sent to instance 0x1004d7b10'
*** Call stack at first throw:
(
0 CoreFoundation 0x00007fff83ed9444 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x00007fff85fbb0f3 objc_exception_throw + 45
2 CoreFoundation 0x00007fff83f321c0 +[NSObject(NSObject) doesNotRecognizeSelector:] + 0
3 CoreFoundation 0x00007fff83eac08f ___forwarding___ + 751
4 CoreFoundation 0x00007fff83ea81d8 _CF_forwarding_prep_0 + 232
5 Foundation 0x00007fff88a5609e +[_NSPredicateUtilities max:] + 46
6 Foundation 0x00007fff8893ce72 -[NSFunctionExpression expressionValueWithObject:context:] + 530
7 CoreData 0x00007fff8613b5b1 -[NSMappedObjectStore executeFetchRequest:withContext:] + 2081
8 CoreData 0x00007fff8613ad10 -[NSMappedObjectStore executeRequest:withContext:] + 80
9 CoreData 0x00007fff86108900 -[NSPersistentStoreCoordinator(_NSInternalMethods) executeRequest:withContext:] + 688
10 CoreData 0x00007fff8610621b -[NSManagedObjectContext executeFetchRequest:error:] + 267
11 FortunesHelperMVC 0x0000000100001d9d -[ImportOperation getMaxID] + 572
12 FortunesHelperMVC 0x0000000100001f95 -[ImportOperation main] + 330
13 Foundation 0x00007fff888f406d -[__NSOperationInternal start] + 681
14 Foundation 0x00007fff888f3d23 ____startOperations_block_invoke_2 + 99
15 libSystem.B.dylib 0x00007fff86a98ce8 _dispatch_call_block_and_release + 15
16 libSystem.B.dylib 0x00007fff86a77279 _dispatch_worker_thread2 + 231
17 libSystem.B.dylib 0x00007fff86a76bb8 _pthread_wqthread + 353
18 libSystem.B.dylib 0x00007fff86a76a55 start_wqthread + 13
)
terminate called after throwing an instance of 'NSException'



Any ideas why this isn't working?

Thanks

Darren.



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: NSWindow's -orderOut: unreliable with Spaces?
  • Next by Date: Re: App works when launched from Xcode, not from Finder
  • Previous by thread: NSWindow's -orderOut: unreliable with Spaces?
  • Next by thread: UITableView / iPhone Calendar List View Question
  • Index(es):
    • Date
    • Thread