core-data NSDate searching for unique days and sorting

放肆的年华 提交于 2019-12-11 15:45:28

问题


Lots of information on NSDates about the place, but I haven't found a clear solution to this.

I have a list of Event entities, each with potentially many EventSessionTimes.

Event <--->> EventSessionTime

In 1 table view I want to display a unique list of days that have Events, in another table view I want to show the events on a particular day (ordered by time).

To achieve this I currently have 2 NSDates - a day and time - and some overly complicated searching/sorting. I want to remove this redundant information.

With that in mind, how can I:

1) Finding all UNIQUE days with events, no concern about specific time

2) Finding all EVENTS on a particular day (duplicates fine here)

Any tips on how to better achieve this would be great.

Thanks.


回答1:


I now have the following relationship. Still have redundant date information..

Event <--->> EventSessionTime <<--> EventDay

1) Finding all UNIQUE days with events, no concern about specific time

I can create specific dates where I know the time. The localised nature of my application means I can do this safely.

...
[NSEntityDescription entityForName:@"EventSessionTime" inManagedObjectContext:self.managedObjectContext];
...
NSArray *res = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
[self myResultArray:[res valueForKeyPath:@"@distinctUnionOfObjects.eventDay.day"]];

You can infer my attribute and relationship names.

2) Finding all EVENTS on a particular day (duplicates fine here)

I use the following predicate to find all events on the specified day with. It does not return duplicates

[NSPredicate predicateWithFormat: @"(SUBQUERY(eventTime, $x, $x.eventDay.day == %@).@count > 0)", <the day selected from the previous list>];


来源:https://stackoverflow.com/questions/2621179/core-data-nsdate-searching-for-unique-days-and-sorting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!