Reputation: 2655
I currently have an app which lists a number of events (horse riding, party, etc), i want to be able to add these events to a list of 'favourites' which will be stored in core data, but i also want these events to be sorted in the order they were added to favourites. I realise i could just add an index property to the event and sort using a descriptor when i retrieve the events but i would like to be able to add events to multiple lists of favourites, so i don't think that would work.
I've also looked into ordered relationships, which is exactly what i am looking for but requires iOS5, as a last resort i could probably cope with that although i would prefer to be able to find another way to do this if possible. Any ideas?
Thanks.
EDIT: The user can also add and remove lists of favourites so adding a date property and sorting by that would not be possible.
Upvotes: 0
Views: 541
Reputation: 6842
The correct solution is to have a 3rd entity that represents a membership of an event to a favourite list. Let's call it EventInFavourites
.
EventInFavourites
has two many-to-one relations:
Favourites
<-------->> EventInFavourites
This one says that a Favourites
can have several Event
in it
Event
<---------->> EventInFavourites
This one says that an Event can be part of several Favourites
lists.
Finally, the position of that event in that favourite list is represented with an attribute of
EventInFavourites
, let's say position
.
So when you want to add an event to a favorite lists, you create an EventInFavourites
instance and you link it to that event and to that favorite list. A bit like this:
EventInFavourites *newFavouriteMembership = [EventInFavourites insertInManagedObjectContext:self.moc];
newFavouriteMembership.event = anEvent;
newFavouriteMembership.favourites = aFavouritesList;
newFavouriteMembership.position = 3; // or whatever
[self.moc save:NULL];
I left out a few details, but that should give you the big picture.
And of course, you can wait for iOS 5.
Upvotes: 2
Reputation: 14694
Go with ordered relationships with iOS 5. iOS devices are updated fairly quickly and I imagine that you would not be forsaking a large percentage of potential customers. Not to mention the time you will save from having to roll your own implementation.
Upvotes: 2
Reputation: 3042
Store the time & date when the item was added to the favorites. Later, when querying the db, order by this timestamp.
With different lists of favorites you might want to store multiple timestamps, one for each list.
Upvotes: 0