I have this situation in my CoreData:
I have entity A.
I have the B entity that has as its parent entity entity A.
I have my TableViewController where I use NSFetchedResultsController to visualize the A objects.
Now, I need to visualize the A and B objects into the same TableView.
Is possible this?
Fetch B and retrieve the As with
bObject.aChildren // or whatever your relationship is called
Related
Maybe this is pushing NSFetchedResultsController too far... anyway, this is what I'm trying to do:
I have the core data classes and relationships: A <->> B <<->> C (A has 1 to many to B, B has many-to-many to C). I'm given one object of type A. I want to use NSFetchedResultsController to give me all reached objects of type C (NOT distinctly - I want duplicates), where the sectionNameKeyPath is the 'name' property of the B object. Possible? How?
I do not think that is possible with a NSFetchedResultsController. A FRC can
only display the result of a fetch request, and a fetch request returns
objects of a given entity without duplicates.
I have an Entity A which has to-many relationships with Entity B.
Entity A -->> Entity B
I need to refer to the count of to-many relationship, at more than one screen. Further, I can remove and add reference to any Entity B from Entity A multiple times.
Now, the question is : What is the best way to refer the relationship count?
What I observed:?
1] I can make a count attribute in Entity A and increment/decrement it according to the relationship count and then fetch this attribute on screens I need.
2] I can also get the count from count property of NSSet(of relationships), this way I do not have to fetch the EntityA. I can simply do,
NSSet *set = EntityA.EntitiesB;
NSInteger count = set.count;
This way also fetch happens but I do not have to create a fetch request again and again for EntityA.
Appreciate any help.
You don't actually have to fetch anything, you can create your fetch request with suitable predicate and then use countForFetchRequest:error: to get the count. You could also create a fetch request template (setFetchRequestTemplate:forName:) and then use fetchRequestFromTemplateWithName:substitutionVariables: when you need to use it.
Use the count on the relationship. This pattern will also fit better when integrating the relationship into the UI (for example, number of rows in a table view), and is the method seens in apple's sample code. Creating a count attribute would most likely just add unnecessary complexity to your model.
Let's say I have two entities, A and B. My goal is to implement the search by all properties of A and just one property of B, and to display sum of search results in one tableView. Naturally I can set fetchRequest only by one entity. But if I am understand right I can fetch not only from A but from B too if they are connected with relationships.
So what do I need to do to implement this? Does default fetch fetches all properties from A and B? If not, how can I specify fetch of all properties from A and one property of B in one fetch?
Issue solved.
At the stage of preloading data from JSON file into Core Data I am define getters for properties (prefetching data from entity)in B and setting entity properties of A equal to returned results of this getters (all this in NSManagedObject subclasses inherited from entities). So after that I am able to fetch all what I need using this getters through appropriate properties.
I have an app with a simple CoreData model that is 3 entities:
B <---> A <<----> C
A is the main object and it has relationships to (aggregates) 2 other entities (B and C).
The UI shows some bits of data from all 3 in the UITableView and the detail view (it's a master/detail app). My problem is that if the user edits the currently viewed item and modifies properties of B or C the NSFetchedResultsController that is keyed on entities of type A does not get called via the NSFetchedResultsControllerDelegate.
Logically, changes in B/C objects is a change to an A in terms of the aggregate being displayed. Is there a way to configure the fetch or the controller so that it will update itself if any of these entity types change?
There are various solutions.
Hack your entities like in the link indicated by Dan Shelley above.
Make your view controllers listen to each other via delegate protocols or notifications and react accordingly. This is the standard way if you do not have Core Data and NSFetchedResultsController involved.
I have an Entity Person. Person has three relationships to Entity ObjectA, ObjectB, and ObjectC.
Can I use an NSFetchedResultController to sort them by type? So section 0 would be ObjectA's, section 1 would be ObjectB's, and section 3 would be ObjectC's? And further, can i sort them by name?
Or do I use 3 different datasources (either load them into an array or 3 different NSFetchResultController's)?
An NSFetchedResultController can only fetch one entity type at a time, so assuming ObjectA, ObjectB, and ObjectC are different entity types, then you'll need three NSFRCs.
If they are three different entity types, you might consider putting them under an abstract entity. Then you could create one NSFRC for that type (example here).