I have 2 entries
1. category (main one)
2. info
the relationship is 1 to many , for every category there is several info's
after i added the several info classes to each categoty
i want to get each category relationship list in the detailsviewcontroller
I created a temp class in the details view the contain the selected category
how do i get access to the list of INFO'S ??
Without seeing your code or model classes, I can't give you the exact answer. But when you made your model, you probably named your relationship something like "infosForCategory". When you generated the model it made a NSMutableSet for that 1-to-many relationship. In that case, you can access the list of infos with:
NSMutableSet *myInfos = myCategory.infosForCategory;
I'm not exactly sure what you're asking but it sounds like you already have an instance of Category and you want to retrieve all the related instances of Info. In that case, Category should have an automatically generated info property that is of the NSSet type. That set will contain all of the related Info objects.
Related
I have 2 entities. One describes the Section of the TableView (A Month its name, etc.) This entity is related with a one to many relationship to another entity which should describe the rows of the TableView.
I'm a bit confused how to get those entites by an NSFetchedResultController. As far as I now I can only fetch one relationship at the time. So which one should I get to fill the table properly?
If you're using NSFetchedResultsController, you fetch the objects you want to display in the table view.
To get sections, you use NSFetchedResultsController's sectionNameKeyPath property to indicate how to find a section name from one of the fetched objects. This key path is something you could pass to one of the fetched objects via valueForKeyPath: to get the section name. In your case it would require traversing a relationship back to the month entity (or whatever it really is) to get its name. For example if the relationship is called month and the Month entity has a name attribute, you would pass something like #"month.name" as the sectionNameKeyPath argument when you create the fetched results controller.
You can also use the excellent Sensible TableView framework to automatically fetch the Core Data objects and display them in a table view. The framework will also detect if the entities have any relationships and will automatically manage the detail view controllers between them.
What is the difference between
ObjectQuery<SalesOrderHeader> query =
context.Contacts.Include("SalesOrderHeaders").Include("SalesOrderDetails");
and
Contact contact =
context.Contacts.Include("SalesOrderHeaders.SalesOrderDetails").FirstOrDefault();
Any advantage of using one over the other?
My main confusion is for the using 2 Include in first one and using 2 tables in second Include.
Thanks in advance.
On entity framework you have something called navigation properties (Named on your .edmx model) so you can access related entities based on them, in other words, you can use your navigation properties to eager load (include) related data..
I'm guessing you have this relationship: One Contact can have many SalesOrderHeaders and one SalesOrderHeader can have many SalesOrderDetails, so if you start loading from contacts entity and want to have both relations loaded you should do:
Contact contact = context.Contacts.Include("SalesOrderHeaders").Include("SalesOrderHeaders.SalesOrderDetails").FirstOrDefault();
as you can see, we're using first include to bring SalesOrderHeaders from DB and the secound one to bring SalesOrderDetails through SalesOrderHeaders navigation property as we're starting from Contacts
So I have this problem I am trying to solve - I wonder if anyone can comment on/help me with the approach. The thing is, I have it partly solved, but with the rest I'm not quite sure.
Here's the deal:
I have a fairly large DB online which I want to load on first start of the App. After that I am only going to load it if new versions exist.
I use an xml parser to parse the data and enter all the data to my data model. The database consists of thousands of products, all described by various attributes.
Anyway, it's easy for me to save thousands of products in a database, then retrieving the data on demand.
I have a problem of how to categorize them and how to save the category data. There is a main category i.e. Hi-Fi which has several subcategories- let's say 'stereo', 'tuner', 'phone' and so on....
How to best save this info, that category a has 15 subcategories and each of these categories in turn has 30 products while securing performance and keeping process-time at a minimum. I don't want to check all 2000 Products whether I need to show them in a certain table view each time I open a new table view.
Any hints on the apporach are appreciated.
You'll need two entities: Product and Category.
Category has a to-many relationship called subcategories with a target entity of Category. The inverse relationship can be called parentCategory. Category also has a to-many relationship called products. Product would have an inverse relationship called category (or categories if a product can belong to multiple categories)
Now, you can get all the products for a given category by checking its products property. If you want to include all the products in the subcategories, you can do a fetch request with a predicate like this:
[NSPredicate predicateWithFormat:#"category == %# OR category IN %#", category, category.subcategories];
I think you can solve it by having a Core Data modal consisting of three entities: Product, Category and SubCategory.
Product has a relationship category with destination Category and a relationship subcategory with destination SubCategory.
Category has a to-many relationship products with destination Product and a to-many relationship subcategories with destination SubCategory.
SubCategory has a to-many relationship products with destination Product and a relationship category with destination Category.
When defining these relationships remember to assign the Inverse relationships as well.
Now you get a list of all products belonging to a specific category by just loading the Category in question and accessing the products property. It should also be possible to use NSFetchRequest for Product with a predicate specifying which category you want. Which is best regarding performance and memory requirements I can't say so you just have to test which approach works best.
I have an app I am working on that has 2 entities linked by a relationship (many to many). I currently have the app set up so that the rootviewcontroller controls the NSManagedObject (getting, setting, deleting of the data) in the first entity. When I drill down into on of the first entities it goes to a secondview (secondviewcontroller) that also has a table on.
When I added a NSManagedObject on this view controller I got an error, which i figured to be because I then have 2 NSManagedObjects. I then resorted to referencing the rootviewcontroller's NSManagedObject which does allow me to get and display entities in the secondview, but since it is referencing the first view it brings back first entities, not second.
How do I manage to have 2 tables on 2 view that access data from entity 1 and entity 2 respectively. As a sidenote, how do I get NSPredicate to filter the results of clicking a row in table 1 to show only entity 2's that are related to the entity 1 that was clicked on.
Sorry for the massive explanation, but it is complicated (to me) and I need to figure it out as I have been doing this for over 3 days and no googling, books, blogs etc have gone into the detail I need! I must be missing something obvious!
Overview:
Entity 1:
name
relationship 1
Entity 2:
name
relationship 1;
rootviewcontroller:
has NSManagedObject
table populated with entity 1's
secondviewcontroller:
has table that needs to be populated with entity 2's that are related to entity 1 clicked on
Currently the instance on rootviewcontroller that is in secondviewcontroller returns a list of entity 1's not 2's.
Thanks in advance guys :)
I know I should post some code of what I have done so far, but as far as I can see what I have written must be wrong from the base up!
I have a Core Data entity called "Item" and it represents an item in a store so it has a name, price, and a few other attributes.
I would like to be able to create lists of these items and I am having some trouble figuring out how to do it.
The problem is that I need to be able to associate a quantity for each item in the list AND I need to be able to add the item to multiple lists.
So for example, say I have an item called "Bread" and I want to add it to two different lists with different quantities associated with each relationship.
I see that the documentation for Core Data says that a userInfo dictionary can be associated with a relationship but I can't seem to locate any information that would indicate whether or not that would work for me.
Any ideas?
This is probably not the best place for a userInfo dictionary. Instead, create a new entity, which has a list releationship, an item relationship, and a quantity attribute. When you add Bread to a list, you actually add this 'link' object, and hook up the Item and List relationships, then set its quantity.