I’ve been trying to come up with a way to solve my problem, but every solution I can think of is messy and makes me want to retch.
I have a one-to-many relationship, consisting of a Team object that can have many Member objects. When I built my data model using Xcode, I was given the default NSSet in which to store the member objects, Unfortunately Sets are not ordered and I need to preserve the order of the Member objects and I need to know if there are empty spaces between Members.
I thought of Using an NSArray in place of the NSSet and creating a dummy Member object in my data store that I could use to mark vacant a spot between to Member objects, but that solution really feels like too much of a hack to me. Since I’ll always have to filter out this dummy Member from any queries.
An NSDictionary would be perfect as I could store the Member object references and their positions as Object-Key pairs, (taking care of both order and vacancies) but apparently CoreData does not support NSDictionary.
Has anyone had a similar need, and devised a simple solution?
Ordered relationships are trivial to implement. Here’s the code I did for one of NSManagedObject subclasses.
(For some reason, I can’t post formatted code to Stackoverflow today.)
In this case I’ve got a relationship set up like:
The AlphaToBetaEntity has an
orderAttribute. I sort the AlphaToBetaEntity and then return the linked BetaEntity as needed. It’s like doing a pointer sort in old school C.It has the advantage of being blistering fast on very large sets and you don’t have to fault in the BetaEntity until you need to read an attribute from it.