本文介绍了获取实体中值的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取核心数据中实体的属性的最大值。苹果有一个很好的例子,如何做到这一点;然而,它不适合我。我有10个对象在我的moc和以下代码总是返回一个大小为0的数组。任何人都可以告诉我我做错了什么?感谢!

I'm trying to get the maximum value for an attribute in an Entity in core data. Apple has a nice example here of how to do this; however, it doesn't work for me. I have 10 objects in my moc and the following code always returns an array of size 0. Can anyone tell me what I am doing wrong? Thanks!

  NSManagedObjectContext* moc = [self managedObjectContext];

  // set the idx to the maximum value
  NSFetchRequest* request = [[NSFetchRequest alloc] init];
  NSEntityDescription* entity = [NSEntityDescription entityForName:@"Transaction"
                                            inManagedObjectContext:moc];
  [request setEntity:entity];

  // Specify that the request should return dictionaries.
  [request setResultType:NSDictionaryResultType];

  // Create an expression for the key path.
  NSExpression* keyPathExpression = [NSExpression expressionForKeyPath:@"idx"];

  // Create an expression to represent the minimum value at the key path 'creationDate'
  NSExpression* maxExpression = [NSExpression expressionForFunction:@"max:"
                                                          arguments:[NSArray arrayWithObject:keyPathExpression]];

  // Create an expression description using the minExpression and returning a date.
  NSExpressionDescription* expressionDescription = [[NSExpressionDescription alloc] init];

  // The name is the key that will be used in the dictionary for the return value.
  [expressionDescription setName:@"maxIdx"];
  [expressionDescription setExpression:maxExpression];
  [expressionDescription setExpressionResultType:NSInteger32AttributeType];

  // Set the request's properties to fetch just the property represented by the expressions.
  [request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];

  // Execute the fetch.
  NSError* error = nil;
  NSArray* objects = [moc executeFetchRequest:request error:&error];
  if (objects == nil) {
    // Handle the error.
  }
  else {
    if ([objects count] > 0) {
      int newIdx = [[[objects objectAtIndex:0] valueForKey:@"maxIdx"] intValue] + 1;
      [self setPrimitiveIdx:[NSNumber numberWithInt:newIdx]];
    } else {
      [self setPrimitiveIdx:[NSNumber numberWithInt:1]];
    }
  }


推荐答案

代码看起来很对我,所以检查下面

Your code looks right to me, so check the following


  1. 你的商店中实际上有Transaction对象。只需从相同的上下文进行正常提取。

  1. You actually have Transaction objects in your store. Just do a normal fetch from the same context.

您可以在设置上下文之前执行此操作吗?

Could you be doing this before you set up the context?

看是否有任何错误 - 只是log [error localDescription]

Check to see if there's anything in error -- just log [error localDescription]

NSInteger32AttributeType is for idx ?

Is NSInteger32AttributeType right for idx?

idx拼写正确吗?是交易?

Is idx spelled correctly? Is Transaction? Meaning, they match your model.

PS:对结果没有影响,但希望你有从代码示例中释放

PS: It won't matter for the result, but hopefully you have the releases from the code sample

这篇关于获取实体中值的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 05:17
查看更多