简体   繁体   English

NSFetchRequest获取属性的最大值。结果每次都不同

[英]NSFetchRequest for an attribute's max value.The result is different every time

NSFetchRequest for an attribute's max value. NSFetchRequest以获取属性的最大值。

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 我为结果日志循环了10次

  • 2012-07-03 11:05:55.831 Edusorf[1956:15803] max time stamp: 136934464 2012-07-03 11:05:55.831 Edusorf [1956:15803]最大时间戳记:136934464
  • 2012-07-03 11:05:55.831 Edusorf[1956:15803] max time stamp: 137170400 2012-07-03 11:05:55.831 Edusorf [1956:15803]最大时间戳: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]最大时间戳:137106624
  • 2012-07-03 11:05:55.832 Edusorf[1956:15803] max time stamp: 137154096 2012-07-03 11:05:55.832 Edusorf [1956:15803]最大时间戳记: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]最大时间戳: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]最大时间戳:137115728
  • 2012-07-03 11:05:55.833 Edusorf[1956:15803] max time stamp: 137110608 2012-07-03 11:05:55.833 Edusorf [1956:15803]最大时间戳: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]最大时间戳:136828592
  • 2012-07-03 11:05:55.834 Edusorf[1956:15803] max time stamp: 136995824 2012-07-03 11:05:55.834 Edusorf [1956:15803]最大时间戳:136995824
  • 2012-07-03 11:05:55.835 Edusorf[1956:15803] max time stamp: 136925072 2012-07-03 11:05:55.835 Edusorf [1956:15803]最大时间戳:136925072

Could anybody helps me out? 有人可以帮我吗? thx~~ 谢谢~~

在数据模型中将ts类型更改为Date。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM