Core data - remove entity if no foreign keys - ios

I am using Core Data in my app and have some entities that have many-to-many relationships. Is there a way to configure the relationships in such way that all entities that don't have at least one entry in some cross-reference table get removed?
Simplified entities:
User:
- songs
- albums
Song
- users
Album
- users
For example, when I remove all users from some album, I want that album to get removed. I know this can be done by checking the number of remaining users, but is there a simpler way?

You can set the delete rule of the relationships in both directions to "Cascade" in the model editor. Core Data should then delete the corresponding entity instances automatically.
You should not follow any advice recommending intermediate "join" tables. This is bad and redundant practice within the Core Data framework and only necessary if you need to store additional information about each relationship itself.

Related

Core Data delete rules, delete all relationships and entities (many to many)

I am having a hard time understanding how Core Data Delete Rules work. I have a House Entity has 2 many to many relationships. The related entities also have their own relationships to other entities.
When I delete the House entity I want to delete all entities that are in relationship to the parent (Room, Door, Window and Decks), and all the entities in relation to those sub entities.
What kind of delete rule do I need for something like this?
The delete rule you are looking for is Cascade
If you want a behavior wherein on deletion of House all other entities are deleted, then you'll need to setup Cascade delete rule on the both ends of the relationships.

How to save array to an attribute of an entity?

I have a question about core data. I have an Entity named User and this entity has 3 attributes: name, images, videos. How can I save an array of multiple images or videos to the attributes images or videos?
With relational databases, whenever you have one attribute that is going to have a bunch of values for a single entity, you have a one to many relationship and you actually need another entity. So in a strict relational database, you would create a new entity, Image, that had one column pointing to the image's data and a second column pointing to the user that that image is associated with, a foreign key. In CoreData, they represent these foreign key columns as a relationship. So you'll have to make a new entity for each 1-to-M(any) relationship you have and give them a relationship back to user, and in turn user to them.
Try this tutorial, it may help you.

Core Data Model

I'm struggling with creating a suitable Core Data model for my app. I'm hoping someone here can provide some guidance.
I have two entities -- "Goals" and "Items". The Goals entity contains only a goal description, but any goal may have any number of subgoals, and these may extend multiple levels in a tree structure. Subgoals are to be contained within the same entity, so presumably the Goal entity will contain a pointer to "parent" which will be the parent goal of any subgoal.
There will also be an "Items" entity that contains a couple of text fields and a couple of binary items, and must be linked (ideally, by a unique identifier, perhaps objectID) to the particular goal or subgoal the item(s) are related to.
I am totally fumbling with how to set this model up. I know what attributes need to be in each entity, but the relationships, particularly between goals and "subgoals", has me stumped. I don't seem to be able to turn up any good examples of tree structures in Core Data on the Internet, and even the couple of books I have on Core Data don't seem to address it.
Can anyone here help an old SQL programmer get headed the right direction with these relationships in Core Data? Thanks.
Have you tried creating a one-to-many from Goal to itself, and a one-to-one from Goal to Item? The only thing I would worry about here is circular references.
Also, read Relationships and Fetched Properties in the CoreData Programming Guide.
Here is how it is done:
You set up a to-many relationship from Goal to Item in the model editor. Don't use any ids, foreign keys etc. This is old-fashioned database thinking - you can forget about it. Here we are only dealing with an object graph. The database layer is just an implementation detail for persisting the data.
Make two more relationships in entity Goal to itself: a to-one called parent, a to-many called subGoals. Make them the inverse of each other. Simple!
QED is correct, you can create a to many relationship on goal (call it subgoals) as well as a to-one relationship on goal (call it parentGoal) and set them as inverses to each other.
Then create another to many relationship (call it items) on the goal entity, with the inverse being a to one relationship on the item entity (call it goal). Then you're all set. You don't need to link items with a unique id, just add them to the items relationship.
Also note that if you did want to give items a unique id, do not use the objectID. The objectID should only be used as a temporary id as they are not guaranteed to remain the same. In fact they will change if you ever do a Core Data migration.
One way, though not really great, is to create a another entity, say subGoal, and each goal has one subGoal and each object of subGoal has many goal.

Delete the User Entity and all the data related to the Entity in Core Data

I have a Core data model like the User Entity having relationship with multiple entities. I need to delete all the entities that is related to User entity when i try to remove the user in core data.
How to achieve this?
The easies way is to go to all the relationships in the Core Data model editor, select each and choose the Delete Rule (which is preset to Nullify) to be Cascade.
Now all depending entities will be deleted as well.
Here is the relevant section in the Core Data Programming Guide.
You need to set the delete rule on your relationships. NSCascadeDeleteRule will cascade deletes through the relationships.
You can find the full details on each delete rule in the Documentation. Click on Organizer/Documentation and type in NSDeleteRule for more details.

How to have repeated items in an Ordered To-Many relationship?

In Core Data of Xcode 4.3.2, an Ordered To-Many relationship is modelled with NSOrderedSet. It works well until I found the need to have repeated items in the relationship; it should really be modelled in a NSArray.
For example, in a music app, I have the following songs: SongA, SongB, SongC ,
I may want a party play list where people insert songs in any order and could be repeated. The list may look something like:
[SongC, SongC, SongA, SongC]
The way Core Data currently works, the list would become:
[SongC, SongA]
I.e., all repeated items are dropped, as it is modelled with sets.
So, coming back to my question: what is a good way to model repeated items in order in a relationship in Core Data?
Relationships are sets (and ordered relationships are ordered sets, but still sets); sets by definition contain unique objects. So you can't put duplicate objects into a relationship either way.
Whether you use the ordered-relation feature or not, you'll want to go back to the abstract ER model to find another way to turn your conceptual relationships into a Core Data model... it might help to think about how you'd do it in a plain SQL (or SQL-like) database and then come back to what Core Data does beyond SQL.
It sounds like you're making something akin to iTunes playlists, no? A model that might work for that would go something like:
Playlist <--->> PlaylistEntry
PlaylistEntry <<---> Song
The PlaylistEntry entity represents one instance of a Song's inclusion in one Playlist. You can have multiple PlaylistEntrys that reference the same Song in a single Playlist, and you can add other attributes to the PlaylistEntry to keep track of other things (like song order, if you're not using an ordered relationship). As a bonus, you can use that to add other features if you like -- say, to make a playlist that plays three different snippets out of one long track.
As rickster said, relationships as managed by Core Data uniquely associate entities between them. Even if Lion's Core Data (is supposed to) supports ordered relationships (supposed to, because in practice it won't work, the feature is buggy, barely usable), they still are relationships that follow the relational database model.
And so you have to manage the association by yourself, and you most certainly have to manage the ordered part of the association by yourself too.
Score <->> ScoreSong
ScoreSong <<-> Song
With ScoreSong having the following properties:
ScoreSong:
- score: -> Score
- song: -> Song
- order: integer, indexed
Then you have to use a Fetch Request with a sort descriptor for the key order, which will return an ordered NSArray of ScoreSong. You can ask the fetch request to prefetch the songs, then you can create the songs array, still properly ordered, with a single call to valueForKey: #"song".
Of course you have to create different ScoreSong for a single Song when you need to include than song more than once in your Score. That's the whole point of the added indirection.

Resources