如何使用核心数据获取每个第n个对象 [英] how to fetch every nth object using core data
问题描述
我有实体问题
。实体问题
具有属性数字
。我需要获取每20个问题:1,21,41,61等。如何做到这一点?
一个 NSFetchRequest
,以及使用模运算符的 NSPredicate
实例,例如以下未经测试的谓词:
NSPredicate * idPredicate = [NSPredicate predicateWithFormat:@modulus:by:(Number,20) 1];
将谓语添加到fetch请求中,并像往常一样执行fetch。
或创建一个 NSSet
的ID值,包括 {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屋!