使用谓词对核心数据进行排序以消除重复

发布于 2024-08-12 00:55:01 字数 2148 浏览 7 评论 0原文

我将一个事件数据库加载到核心数据中,该数据库具有重复的事件标题。这样做是为了让数据库能够提供活动每一天的独特信息。例如,每个日期的定价波动。

我现在需要从列表中删除重复的事件标题,该列表将显示为表视图,并使用 NSFetchRequest 和 NSPredicate 来提供过滤器。但我见过的所有示例都需要将非动态键值用作谓词过滤器的目标。例如,下面的 NSDate 提供了现在的时间作为关键过滤器并且它可以工作。

当前 NSString * title 的目标是事件 ManagedObject 类中返回 nil 值的值。这是 FetchResultsController 的一个片段。

- (NSFetchedResultsController *)fetchedResultsController {
    if (fetchedResultsController == nil) {
        NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
        NSPredicate *predicate = [[[NSPredicate alloc] init] autorelease];
        [fetchRequest setReturnsObjectsAsFaults:NO];  
        [fetchRequest setEntity:[NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext]];
        NSArray *sortDescriptors = nil;
        NSString *sectionNameKeyPath = nil;
        NSDate *date = [NSDate date];
        NSString *title = [events title];
        if ([fetchSectioningControl selectedSegmentIndex] == 1) {
            predicate = [NSPredicate predicateWithFormat:@"(closeDate >= %@) AND (title == %@)", date, title ];
            sortDescriptors = [NSArray arrayWithObjects:[[[NSSortDescriptor alloc] initWithKey:@"category.name" ascending:YES] autorelease], [[[NSSortDescriptor alloc] initWithKey:@"openDate" ascending:YES] autorelease], nil];
            sectionNameKeyPath = @"category.name";
        } else if ([fetchSectioningControl selectedSegmentIndex] == 0){
            predicate = [NSPredicate predicateWithFormat:@"closeDate >= %@", date];
            sortDescriptors = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"openDate" ascending:YES selector:@selector(compare:)] autorelease]];
            sectionNameKeyPath = @"day"; 
        }
        [fetchRequest setPredicate:predicate];
        [fetchRequest setSortDescriptors:sortDescriptors];
        fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:sectionNameKeyPath cacheName:@"EventsCache"];
    }    
    return fetchedResultsController;
}

I have an Event database loaded into Core Data that has duplicate Event titles. This has been made so the database can provide unique information for each day of the event. Eg fluctuations in pricing on each date.

I now need to remove the duplicate event titles from a list that will be displayed as table view with NSFetchRequest and NSPredicate to provide the filter. But all the examples I've seen require a none dynamic key value to be used as a target for the predicate filter. eg below NSDate provides the time now as a key filter and it works.

Currently NSString * title targets a value in the events ManagedObject class that returns a nil value. Here is a snip from the FetchResultsController.

- (NSFetchedResultsController *)fetchedResultsController {
    if (fetchedResultsController == nil) {
        NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
        NSPredicate *predicate = [[[NSPredicate alloc] init] autorelease];
        [fetchRequest setReturnsObjectsAsFaults:NO];  
        [fetchRequest setEntity:[NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext]];
        NSArray *sortDescriptors = nil;
        NSString *sectionNameKeyPath = nil;
        NSDate *date = [NSDate date];
        NSString *title = [events title];
        if ([fetchSectioningControl selectedSegmentIndex] == 1) {
            predicate = [NSPredicate predicateWithFormat:@"(closeDate >= %@) AND (title == %@)", date, title ];
            sortDescriptors = [NSArray arrayWithObjects:[[[NSSortDescriptor alloc] initWithKey:@"category.name" ascending:YES] autorelease], [[[NSSortDescriptor alloc] initWithKey:@"openDate" ascending:YES] autorelease], nil];
            sectionNameKeyPath = @"category.name";
        } else if ([fetchSectioningControl selectedSegmentIndex] == 0){
            predicate = [NSPredicate predicateWithFormat:@"closeDate >= %@", date];
            sortDescriptors = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"openDate" ascending:YES selector:@selector(compare:)] autorelease]];
            sectionNameKeyPath = @"day"; 
        }
        [fetchRequest setPredicate:predicate];
        [fetchRequest setSortDescriptors:sortDescriptors];
        fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:sectionNameKeyPath cacheName:@"EventsCache"];
    }    
    return fetchedResultsController;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

っ〆星空下的拥抱 2024-08-19 00:55:01

您可以设置

setReturnsDistinctResults:YES

fetchRequest。

有关更多信息,请参阅文档:
NSFetchRequest 类参考

You could set

setReturnsDistinctResults:YES

on your fetchRequest.

For more, see the docs:
NSFetchRequest Class Reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文