I have two entities CIDMPost and CIDMUser. CIDMPost has a one-to-many relationship with CIDMUser named invitees.
Now I have an instance of CIDMPost lets say postObject from where I can easily get the invitees by writing postObject.invitees which will return NSSet.
Now my requirement is I want those invitees (postObject.invitees) as NSFetchedResultsController to show in a UITableView with style Group. Also its need to satisfy the below.
Grouped by invitationStatus (an attribute of CIDMUser)
Order by invitationStatus ASC
Declaration:
I tried few things to fulfil my requirement but got a crashed and the reason was Invalid to many relationship. To know the solution of this crash I posted 'a question' an hour ago. But after few conversation with Daij-Djan I had come to know that the whole process I was trying is wrong.
Just fetch the users in your fetched results controller and apply the sort descriptors as desired filter by invitation with a predicate. Use the status field as the sectionNameKeyPath argument when creating the fetched results controller.
NSPredicate(format: "%# in invitations", post)
where post is the post object to which you want to display the invitees, and invitations is the reverse to-many relationship to the post to which users are invited.
(Note that I am assuming a user can get invited to more than one post, which seems logical. In your problem statement you mention a one-to-many relationship which it seems to me should thus be many-to-many).
Related
Update: Answered my question by taking advantage of a to-one relationship going the reversed way. Would love to know if there is a way to expand the selection of the fetchRequest to include other entities.
My current fetch request in Swift grabs from the main table, lets call it mainTable, and filters it using an in-between entity, let's call it betweenTable, based on a single object in my third entity, thirdTable. This works. Here's how it's set up.
fetchRequest = FetchRequest<Screen>(entity: mainTable.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \mainTable.displayName, ascending: true)],
predicate: NSPredicate(format: "betweenTable.thirdTable CONTAINS[cd] %#", selectedThirdItem))
This fetch is based on a condition on the betweenEntity table (a to-many relationship) based on a value on the thirdTable (a to-one relationship). I want to grab a value from the betweenEntity table to use in my list (where the results from this fetch are shown) BUT when I try to index into the between table in the list my build times out. I was hoping there was a way to expand the values selected in the fetch request to include more than just the mainTable entity. For example select mainTable.value, betweenTable.value from ... where...
Can't find any information on this online and was hoping it was possible.
Thanks for any help.
More Information:
mainTable ->> betweenTable is a to-many relationship.
mainTable -> thirdTable is a to-one relationship but to-many reversed.
My main fetch request is for a list that I created as my previous version did live-update and needed to be forced.
Within the list I tried to get the value needed with a mainTable.betweenTable.first(where: ... type expression but then I couldn't build it so I hoped I could add a value in the select for the fetch request. Error: The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
I had started looking into the NSFetchRequest.IncludesSubentities property but couldn't find anything helpful.
I ended up fixing this by switching my query to grab from the betweenTable entity and then follow the to-one relationship back to the mainTable for display purposes. This way I can display values from both tables. The predicate became a simple predicate: NSPredicate(format: "thirdTable == %#", selectedThirdItem)
I would really appreciate some help with using NSFetchResultsController.
I think what I am need to do should be simple for some people, but I am a bit stuck!
I will try to explain what I am doing, or a simplified version of it.
I have 2 viewControllers, each displaying an NSTableView. I am using Core Data, with an entity Clubs. One of the viewControllers displays a list of Clubs. So for this one, I create an NSFetchResultsController, passing to it a fetchRequest which is basically NSFetchRequest<Clubs>(entityName: “Clubs”).
Each club has a list of Members, with a one-many relationship). I want to display the members using the 2nd viewController. So when I tap a club in the first table, I want the second table to show its members.
I have an NSFetchResultsController connected to the second table. I am stuck at what fetchRequest to send to it. If I use a similar one to the first, i.e. NSFetchRequest<Members>(entityName: “Members”), as expected, I end up with one table showing all of the clubs stored, and the second table showing ALL members stored, whereas I want only the members in the club selected in the first table. I hope that makes sense. So my question is, what fetchRequest should I send to the 2nd tables NSFetchResultsController?
You should add the following predicate to the fetch request underlying the FRC:
fetchRequest.predicate = NSPredicate(format:"club == %#", theClubSelectedInTheFirstViewController)
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.
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.
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.