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)
Related
My main Entity is called Series. Series has a one to many relationship to an entity called Rounds, connected by one-to-many (ordered), and Rounds has again an NSOrderedSet with Shots. The Shots entity has an attribute called score, which is an Integer.
What I want, is to get all the scores from the Shots entity, belonging to a specific Series.
let shots = currentSeries.rounds.shots as [Shots]
does not give me all the Shots to iterate through, due to the error messeage: "Value of type 'NSOrderedSet' has no member: 'shots'". Somehow, I need to set a predicate to the "Shots" entity which filters all the Shots that belong to a specific "Series" entity.The Series entity does not have a unique identifier, but I guess it could be possible to use the timestamp attribute to isolate a specific "Series". But again, I want all the "Shots" entities, connected to that specifi "Series".
I could seriously need som help about CoreData mining, or at least som suggestions about how to accomplish my efforts.
One way to get all the Shots for a given Series would be to fetch with a predicate:
let fetch = NSFetchRequest(entityName:"Shots")
let fetch.predicate = NSPredicate(format:"rounds.series == %#", currentSeries)
let currentSeriesShots = try! context.executeFetchRequest(fetch) as! [Shots]
(You should add proper error handling). No need to use a unique identifier - CoreData will use its own (internal) identifier for the currentSeries object to determine which Shots to return.
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).
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've got a problem with UITableViews and many-to-many relationships.
I have two classes, A and B, that both extend NSManagedObject and live in Core Data. Each has a to-many relationship to the other:
A<<-->>B
I would like to use objects of class A as the sections of a UITableView, and it's set of B objects as "data rows" in the TableView. Is this possible, and what would be the best approach to achieve this? Preferably I would like to use a NSFetchedResultsController to manage the data presented by the UITableView, but so far I have not been able to come up with a predicate and section name key path that works.
You can't use a fetched results controller to do this.
If the A objects are the section names and each B objects has many A relationships then each B can show up in the table multiple times in different sections. Core Data is really set up around the idea of unique objects and all the boilerplate code for tableviews assumes that each row represents a single unique object. In this circumstance, even counting the number of rows would be difficult and the sectionNameKeyPath would be impossible to set because it could have multiple values at any one time.
You need to configure all this by hand. You'll have to fetch the A objects and then set the section titles, then you will have to get each A.bObjs and count and sort them for the rows in each section completely individually. Deleting an exist B object might prove difficult because it could trigger the removal of multiple rows at the same time.
I would urge you to rethink your design. Sections and section titles are not supposed to represent managed objects but rather a grouping of the managed objects represented by the rows based on one of the row object's attributes. What you really want here is a master-detail view hierarchy. The top tableview will show all the A objects. When a row is selected, it loads the second tableview that shows all the B objects related to the selected A object.
I would use NSFetchedResultsController to watch over the A objects. Use your fetched results to populate the section titles. Then when you are looking to populate your "data rows", you can find your current A object (by using [[fetchedResultsController fetchedResults] objectAtIndex:indexPath.section] or something similar) and extract the B objects from it with A.BOjbects, which returns the NSSet of all B objects associated with the A object. After doing that, you can filter or sort the list as you please before placing them in the rows
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!