I have category entity and subcategory entity. I need to get subcategory data to that related categoryId of category entity.
My category entity contains these attributes: "categoryId","categoryName"
subcategory contains: "subcategoryId","subcategoryName", "categoryId".
So can any one please guide me how can i put the relationship between this two entity ??
Thanks for advance.
Very first you are mixing the concepts of MySQL or SQLite with Core data. Unlike them Core Data does not have primary-foreign Key concept to relate entities(for easy understanding tables in MySQL). Just create relationship between those entities and you can fetch data their data.
Now about your Entities you have are category and subcategory. So you have to create relationship between them. If one category have many subcategories you have to check To many relationship option from Data Model Inspector..Otherwise One to One relationship would be the one you should go for..Have a look at screenshot how you can make your relationship one to many..
This is a good tutorial link for one to many relationship. You can refer if you do not know how to implement.
Also this for tutorial simple relationship in Core Data..
if you are beginner with Core data you can go with Quick Tutorial Start by Apple and you will get basic idea of Core data.
Related
Here above shows the ER-Diagram.
It's easy to implement to-many binary relationship using core data. But it confuse me how to implement this kind of multiple relationship.
Hope that someone could give a hand.
I am not sure if I understood your problem correctly, but wouldn't two
one-to-many relationships (from Course to TA and from Student to TA) describe
your model? Each TA has exactly one Student and one Course, but each Student and
each Course can be related to many TAs.
I have a problem with a entity framework model. I couldn't find the answer also because i really don't know how to explain it well.
I have a User table anad a Rights table. There is a many-to-many relationship.
The EF User model has two properties: GrantedRights and RevokedRights.
From what i understood EF should create 2 tabel (one for the granted and the other for the revoked) with just the RightsID and the UserID.
But I can't find those tables, neither the context gives me the option. I didn't create the DB.
I'd like to know if there's an example code to follow to extract the list of GrantedRights from a user in this case..
Thanks
I asked this question earlier, but I'm missing one important thing. I have a NoteObject entity and every note, without Core Data, just has an arrayOfTags (which is an array of NSStrings). I decided to use to-many relationships to store the tags instead of an array. So I created a new "Tags" entity, and set up a to-many relationship from my NoteObject to Tags. This works great when every tag is related to only one note, but I'm unsure how I would go about linking one tag with multiple notes. How would I set up that relationship?
In you data modeler just make the Tags -> Notes relationship a to-many relationship. If you have already generated the Entity subclasses you will have to redo those. You will also have to be careful when creating tags to ensure uniqueness, but other than that it should be fairly straightforward. myTag.notes should work just as well as myNote.tags.
I have set up Core data in my application, with two entities: Restaurant and Menu. There are about 30 or so restaurants, and 6 menus. One menu can belong to several restaurants.
However, each Restaurant entity has a menuId field. Is there a way to retrieve the specific menu entity according to which menuId the Restaurant entity has?
I also have a problem with values being stored more than once, even though they are the same. How can I prevent that from happening?
I'm pretty new to Core data, so any best-practice tips would be appreciated!
Thanks!
You could use an NSFetchRequest to retrieve menus by their id, but it would actually be better to define a relationship from your Restaurant entity to your Menu entity (and vice-versa). Internally, that does pretty much the same thing as your menu id attribute, but it's much more convenient to use.
You're following a database approach, based on tables and IDs to link these tables. This is wrong in Core Data, you must follow a object graph approach and CoreData will transform it to a corresponding SQlite database in a manner completely transparent to you.
So given the entities °Restaurant" and "Menu" you will create:
- in the entity "Restaurant" a 1-to-many relationship to "Menu" called "menus"
- in the entity "Menu" a 1-to-many relationship to "Restaurant" called "restaurants"
You will just need to add each restaurant menu to the NSSet corresponding to the menus relationship for a given menu, and vice versa. As you can see in this way you don't need to care about IDs, but just the object relationships.
As far as repeated data, this depends on you. Normally you should have a way to identify the unicity of an object (e.g.: the menu name, the restaurant name) and then try to retrieve it from Core Data before adding it as a new object.
If a Core Data relationship has an inverse relationship do you only need to set one of the relationships objects then the corresponding relationship is setup?
In the past I have set both relationships but when looking at iPhone Core Data Recipes it seems they only set 1 of the relationships?
Thanks
James
I found the answer in the documentation:
Since Core Data takes care of the object graph consistency maintenance for you, you only need to change one end of a relationship and all other aspects are managed for you. This applies to to-one, to-many, and many-to-many relationships. Consider the following examples.
So the answer is YES you only need to set one side of the relationship.
More information here:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdRelationships.html#//apple_ref/doc/uid/TP40001857-CJBDBHCB