如何对 fetchRequest 上的第一次出现进行谓词

发布于 2025-01-06 19:27:46 字数 93 浏览 0 评论 0原文

我正在尝试找到一种方法来预测某一天事件的第一次发生。假设一个事件在一天中发生多次(范围 1-16),我只希望它返回第一次出现的事件,然后在第二天查找下一个第一次出现的事件。

I'm trying to find a way of predicating for the first occurrence of an event on a given day. So say an event occurs multiple times during the day (range 1-16), I only want it to return the first occurrence and then look for the next first occurrence on the following day.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅哥哥的热头脑 2025-01-13 19:27:46

您无法通过一个 fetchRequest 来完成此操作;你需要做两个。

对于每一天,只需使用谓词来限制当天开始后的出现次数,按日期升序排序,然后仅获取一项:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"YourObject" inManagedObjectContext:context];
request.predicate = [NSPredicate predicateWithFormat:@"date > %@", beginningOfDay];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]];
request.fetchLimit = 1;

当然,您需要检查获取的项以确保它仍然存在同一天。然后,第二天重复。

You won't be able to do this with one fetchRequest; you'll need to do two.

For each day, just use a predicate to limit to occurrences after the beginning of that day, sort in ascending order of date, and fetch only one item:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"YourObject" inManagedObjectContext:context];
request.predicate = [NSPredicate predicateWithFormat:@"date > %@", beginningOfDay];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]];
request.fetchLimit = 1;

Of course, you'll want to check the item you get to make sure it's still on the same day. Then, repeat for the next day.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文