如何找到前20个访问过的项目来添加它们的最爱

[英]how to found top 20 visited items to add them ad favorites


Im Using core data. in that i was worked on recently visited items as follows.

我正在使用核心数据。因为我在最近访问过的项目上工作如下。

//Create a fetch request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entityDescription = [NSEntityDescription 
                               entityForName:@"Recent" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entityDescription];

//set Predicate to the fetch request
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"eyeTerm = %@",term];
[fetchRequest setPredicate:predicate];
NSError *error = nil;

//Create an Array Using fetch Request
NSMutableArray *recentsArray = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy];
if([recentsArray count]==0) {
    //Save to Recent
    Recent *recent = [NSEntityDescription insertNewObjectForEntityForName:@"Recent" inManagedObjectContext:self.managedObjectContext];
    recent.eyeTerm=term;
    recent.lastviewed = [NSDate date];
    recent.repeatCount = 0;
}

else {
    //Increment the repeatCount of the recent object.
    Recent *recent = [recentsArray objectAtIndex:0];
    int countValue = [recent.repeatCount intValue];
    countValue ++;
    recent.repeatCount = [NSNumber numberWithInt:countValue];
    NSLog(@"repeat count : %@",recent.repeatCount );
}

Now i want top 20 visited tiems (repeat count). and then i can add them as favorites. please tell me how to found top 20.

现在我想要前20名访问过的tiems(重复计数)。然后我可以将它们添加为收藏夹。请告诉我如何找到前20名。

Thanks

1 个解决方案

#1


1  

You'll need a sort descriptor:

你需要一个排序描述符:

NSSortDescriptor *sortByRepeats = [NSSortDescriptor sortDescriptorWithKey:@"repeatCount" ascending:NO];

Then you can use that when you create your fetch request:

然后,您可以在创建获取请求时使用它:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];   NSEntityDescription *entityDescription = [NSEntityDescription                                   entityForName:@"Recent" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entityDescription];   
[fetchRequest setFetchLimit:20];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortByRepeats]];
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2011/12/20/6f963bdf2816180ae32c148b2d731a6f.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告