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.
Related
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
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 NSManagedObject A, which has a to many relationship with object B.
If I create object B, and add it to relationship for A TWICE for example
B = get somehow
[A addObjectB:B];
[A addObjectB:B];
Will the graph be still be consistent or do I have to make sure that I do not duplicate relationship in some way? I know it's a set that manages relationship so duplicates should not be allowed, but I just want to make sure.
A "to-many" relationship is represented by a NSSet and is unique.
If you add an object to a relationship set multiple times it will only appear in the relationship once.
I have two entities in my model, A and B. A has a to-many relation to B, and its inverse is a to-one relation back to A. I'd like to fetch results of entity B using A as follows:
Perform a fetch request on entity A with some predicate.
On the results returned by this request, drill through to all the related Bs and on these filter by a second predicate.
Return all the valid results of entity B.
Of course I can do (1) and then filter an array of Bs using the second predicate. However I know this is suboptimal.
How can I do this most efficiently, even with a single fetch and predicate?
Unfortunately, I'm not really sure what you are asking. It surely would help if you gave more details.
So, I'll just have to guess... Keep in mind the fetch request can only return one type of entity. So, if you want the thing that is the many side of the relationship, fetch that.
You can use 'dot' notation in your predicate... I'd also probably do the search backwards...
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"Employee"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:#"department.name like 'Support'"];
There is 1-to-many relationship from Department to Employee. The above grabs all employees that belong to a department with 'Support' in the name.
If you are already holding an instance of A, just access the related B instances through A's accessor.
If you need to directly fetch all the B's related to a particular A (you don't in this case), you'd build a fetch request for the B entity, with a predicate based on the (inverse) relationship of Bs to A. (The specific syntax will depend on the inverse relationship name, and whether that inverse is a to-one or to-many.)