English 中文(简体)
我怎么能够只从核心数据中挑选20大目标。
原标题:How can I fetch only the top 20 objects from core data

我需要找到用户最近看到的20个物体(最新版)。 每个物体在称为日期的数据模型中都有财产。 当用户认为某一物体时,该日期被分配到目前的邮票。

因此,我最近的看法表明,最近看到的20个物体。 目前使用以下代码计算和分类数据。

            [fetchRequest setEntity:[NSEntityDescription entityForName:@"object" inManagedObjectContext:self.moc]];
            predicate = [NSPredicate predicateWithFormat:
                                      @"objectNumber contains[cd] %@", searchTerm]; 

            [fetchRequest setPredicate:predicate];
            NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dateVisited" ascending:NO];
            NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
            [fetchRequest setSortDescriptors:sortDescriptors];
            [sortDescriptors release];
            [sortDescriptor release];

            NSFetchedResultsController *controller = [[NSFetchedResultsController alloc]
                                                      initWithFetchRequest:fetchRequest
                                                      managedObjectContext:self.moc
                                                      sectionNameKeyPath:nil
                                                      cacheName:nil];
            [fetchRequest release];

该法典归还了3 000个物体,我显示第20个。 然而,它分解了所有3,000件物品,需要时间这样做。 如果只有20个顶级物体的轨迹保持下去,如果该目标迄今没有达到20个顶点,则按此方向逐个放弃,则会提高效率。

因此,我的问题是: 是否有办法只追踪20个顶级物体。

最佳回答
[fetchRequest setFetchLimit:20];

Cheers!

问题回答

暂无回答




相关问题
How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

Can I Nest OrderBy in .NET?

This doesn t seem to work as I intend. VB.NET: Dim x = Model.Discussions.OrderByDescending(Function(d) d.Messages.OrderByDescending(Function(m) m.Sent).First.Sent) For Each d As Discussion In x ....

sorting elements javascript

I m looking for a way to sort my elements, but it isn t as easy as it sounds. Please let me explain My elements are grouped per 6 elements (thumbnails), each x represents a thumbnail However all ...

热门标签