如何使用核心数据获取每个第n个对象 [英] how to fetch every nth object using core data

查看:196
本文介绍了如何使用核心数据获取每个第n个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有实体问题。实体问题具有属性数字。我需要获取每20个问题:1,21,41,61等。如何做到这一点?

解决方案

一个 NSFetchRequest ,以及使用模运算符的 NSPredicate 实例,例如以下未经测试的谓词:

  NSPredicate * idPredicate = [NSPredicate predicateWithFormat:@modulus:by:(Number,20) 1]; 

将谓语添加到fetch请求中,并像往常一样执行fetch。



或创建一个 NSSet 的I​​D值,包括 {1,21,41, ...} 并使用谓词:

  NSSet * idSet = / * set NSNumber实例包括1,21等等。* / 
NSPredicate * idPredicate = [NSPredicate predicateWithFormat:@Number IN%@,idSet];

我没有测试过,但是任何一种方法都应该让你靠近。 >

I have entity Question. Entity Question has property Number. I need to fetch every 20th question: 1, 21, 41, 61, etc. How can I do this?

解决方案

Create an NSFetchRequest as usual, as well as an NSPredicate instance that uses a modulus operator, e.g. the following untested predicate:

NSPredicate *idPredicate = [NSPredicate predicateWithFormat:@"modulus:by:(Number, 20) == 1"];

Add the predicate to the fetch request, and do the fetch, as usual.

Or create an NSSet of ID values that include {1, 21, 41, ...} and use that with the predicate:

NSSet *idSet = /* set of NSNumber instances including 1, 21, etc. */
NSPredicate *idPredicate = [NSPredicate predicateWithFormat:@"Number IN %@", idSet];

I haven't tested this, but either approach should get you close, I think.

这篇关于如何使用核心数据获取每个第n个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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