Ios 来自NSFetchedResultsController的随机结果

Ios 来自NSFetchedResultsController的随机结果,ios,core-data,nspredicate,nsfetchedresultscontroller,nssortdescriptor,Ios,Core Data,Nspredicate,Nsfetchedresultscontroller,Nssortdescriptor,我的实体中有一些数据。如何以随机顺序获取所有条目。我想一次又一次地洗牌所有的条目 我在这里找到了这个问题的解决方案:。但是我想获得带有结果的NSFetchedResultsController。有什么想法吗?可能使用NSPredicate或NSSortDescriptor?子类NSSortDescriptor更改默认排序行为。像这样的方法应该会奏效: @interface RandomSortDescriptor : NSSortDescriptor @end @implementation

我的实体中有一些数据。如何以随机顺序获取所有条目。我想一次又一次地洗牌所有的条目


我在这里找到了这个问题的解决方案:。但是我想获得带有结果的
NSFetchedResultsController
。有什么想法吗?可能使用
NSPredicate
NSSortDescriptor

子类
NSSortDescriptor
更改默认排序行为。像这样的方法应该会奏效:

@interface RandomSortDescriptor : NSSortDescriptor 
@end

@implementation RandomSortDescriptor

- (id)copyWithZone:(NSZone*)zone
{
    return [[[self class] alloc] initWithKey:[self key] 
                           ascending:[self ascending] selector:[self selector]];
}

- (id)reversedSortDescriptor
{
    return [[[self class] alloc] initWithKey:[self key] 
                           ascending:![self ascending] selector:[self selector]];
}

- (NSComparisonResult)compareObject:(id)object1 toObject:(id)object2 
{
    NSUInteger random = arc4random_uniform(3);
    switch (random)
    {
       case 0:
          return NSOrderedSame
       case 1:
          return NSOrderedDescending;
       case 2:
          return NSOrderedDescending;
     }
}

@end

当你想要颠覆这一分类时,你需要什么样的FRC?只是变化检测?另一个问题已经有很多建议的解决方案,您尝试了哪些,它们是如何工作的?一些使用
NSPredicate
的优雅解决方案。如果我将nil设置为
NSFetchedRequest.predicate
,我会看到所有条目,但这些结果的顺序总是相同的。我不知道。如果只是使用不同的排序。但是它们的数量不是无限的。同样,如果我使用谓词,并不是所有的条目都被输出。FRC需要一个排序描述符(它只能对实体的持久属性进行排序),因此FRC结果总是非常确定的。这取决于内部的排序算法,可能会产生有偏差的结果。看见如果需要无偏结果,请将随机值指定给实体上的排序属性,然后按该属性排序。