核心数据 - 如何获取具有最大值属性的实体 [英] Core Data - How to fetch an entity with max value property

查看:101
本文介绍了核心数据 - 如何获取具有最大值属性的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个拥有 personId (personId是唯一的)

I have a entity Person with a property personId (personId is unique)

如何获取最大个人ID的人?

How can I fetch the Person with the max personId?

(我想获取人本身而不是属性的值) / p>

(I want to fetch the person itself not the value of the property)

推荐答案

您将 fetchLimit 设置为 1 并按降序排序 personId 。例如:

You set the fetchLimit to 1 and sort by personId in descending order. E.g.:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Person"];

fetchRequest.fetchLimit = 1;
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"personId" ascending:NO]];

NSError *error = nil;

id person = [managedObjectContext executeFetchRequest:fetchRequest error:&error].firstObject;

这篇关于核心数据 - 如何获取具有最大值属性的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆