NSFetchRequest for an attribute’s max value.
But the result turns to a different one every time.
This is my code.
- (NSInteger)getMaxTimeStamp
{
NSManagedObjectContext *context = [delegate managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Relation" inManagedObjectContext:context]];
[fetchRequest setIncludesPropertyValues:NO]; //only fetch the managedObjectID
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"ts" ascending:NO];
[fetchRequest setFetchLimit:1];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
NSError * error = nil;
Relation *relation = [context executeFetchRequest:fetchRequest error:&error].lastObject;
//error handling goes here
if (error) {
NSLog(@"ERROR: %@",[error localizedDescription]);
}
if (relation.ts) {
return (NSInteger)relation.ts;
}else {
return 0;
}
}
and I looped 10 times for result logs
- 2012-07-03 11:05:55.831 Edusorf[1956:15803] max time stamp: 136934464
- 2012-07-03 11:05:55.831 Edusorf[1956:15803] max time stamp: 137170400
- 2012-07-03 11:05:55.832 Edusorf[1956:15803] max time stamp: 137106624
- 2012-07-03 11:05:55.832 Edusorf[1956:15803] max time stamp: 137154096
- 2012-07-03 11:05:55.833 Edusorf[1956:15803] max time stamp: 137107392
- 2012-07-03 11:05:55.833 Edusorf[1956:15803] max time stamp: 137115728
- 2012-07-03 11:05:55.833 Edusorf[1956:15803] max time stamp: 137110608
- 2012-07-03 11:05:55.834 Edusorf[1956:15803] max time stamp: 136828592
- 2012-07-03 11:05:55.834 Edusorf[1956:15803] max time stamp: 136995824
- 2012-07-03 11:05:55.835 Edusorf[1956:15803] max time stamp: 136925072
Could anybody helps me out? thx~~
change the
tstype to Date in the data model.