Question

I have two entities in my Core Data model: Library and Book. They are linked by a To Many relationship between Library and Book.
However, in my example I have the exact same book twice in my library and when I try to add them to my Library, only one appears. I search a bit and noticed that in the Library object, books are stored as a NSSet.
Does anyone know how I could store the books in a NSArray instead ?

Was it helpful?

Solution

You can't (well, unless you do away with the Book entity and use a transformable attribute but that isn't a good solution).

The obvious choice would be to add a count attribute onto your Book entity so that you can record how many of that book you have. What impact this might have on the rest of your design is something you need to consider.

OTHER TIPS

In Xcode sidebar, there is a checkbox named "Ordered relationship", but as Wain mentioned, this won't solve the duplicate issue. But, it will make it save the books in an ordered collection (NSOrderedSet).

I would make a new entity, LibraryBook, that had a to-one relationship with Book, and to-one to Library. You could than have two LibraryBooks in one Library, both pointing at the same Book.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top