【发布时间】:2014-07-23 08:48:29
【问题描述】:
我需要从核心数据文件中获取随机对象,比如 10 个随机对象。
我该怎么做?
这是我的NSFetchRequest 设置
- (NSFetchRequest *)fetchRandom
{
if (!_fetchRandom) {
_fetchRandom = [[NSFetchRequest alloc] init];
}
[_fetchRandom setEntity:[NSEntityDescription entityForName:@"Contacts" inManagedObjectContext:self.oneManagedObjectContext]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"checked = %@", @"0"];
[_fetchRandom setPredicate:predicate];
NSUInteger count = [self.oneManagedObjectContext countForFetchRequest:_fetchRandom error:NULL];
NSUInteger random = (arc4random() % 5) + 3;
[_fetchRandom setFetchLimit:random];
return _fetchRandom;
}
【问题讨论】:
标签: ios core-data nsfetchrequest