Core Data:Fetch rlelationships or Fetch Main Entity - ios

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.

Related

Iterate over CoreData to-many relationship in deterministic order

Is there a way to iterate over a core data relationship in deterministic order ? I don't care what the order is, and I don't want to manage it myself, I just want to be able to rely on the fact that two subsequent calls to this ordering will give me the same result (assuming no object insertion/deletion in between).
Just mark the relationship as ordered, that will be mapped to an NSOrderedSet in your NSManagedObject subclass. You can access the ordered set just like an array, the order will always be kept the same: https://developer.apple.com/documentation/foundation/nsorderedset

how to join tables with NSManagedObject

I've 2 NSManagedObjects Events and PrivateEvents, Where PrivateEvents is the subset of the Events. I just want to show only those Events which are not present in PrivateEvents, in a UITableView.
Please let me know who it can be done.
Why not have a single kind of entity Event with a flag isPivate? This way you could get them with a single fetch request. If you use 2 separate entities, I believe you will have to do the filtering in code, you cannot do a JOIN on the entities.

Core Data: NSFetchedResultsController sorting by date vs. ordered to-many relationship

I am writing a demo messaging app to learn Core Data. In my model, an entity Conversation has a to-many relationship messages to Message. A Message has a timeStamp. I intend to display these messages in a collection view/table view with an NSFetchedResultsController. I want to know what would be the most efficient way to sort these messages. I searched around and found that I could sort the messages using:
an NSSortDescriptor on message.timeStamp
making the messages relationship ordered
adding a sequence property to message
What should be the best way to sort the messages?
The sort order should reflect your application logic:
is it logical to move messages inside a conversation? (if yes, sort by sequence number)
Do you want your data to be displayed chronologically? (if yes, sort by timeStamp)
in any case I wouldn't use the ordered relationship as a sort order as you would probably want an FRC to display your data in a table view, and he will manage memory and changes for you.
In addition to that you would want to batch fetch this relationship in any case and not simply let your table view datasource be the ordered set relationship (which will cause the items to be fetch one by one).

Difference between Fetch Requests (model templates) and Fetched Properties in Core Data

What's the difference between them? Both need predicate. I've used them both but I can't understand the difference. Thanks! (I think it doesn't matter, but I'm working in iOS, Xcode 4 last version).
A fetched property is kind of a relationship. It's not a direct, two-way relationship, it's a one-way relationship. So only one one object knows about the relationship. The fetched property is (normally) described by a predicate and uses a fetch request to retrieve the objects.
A fetch request retrieves objects from core-data. The actual instances of an entity. It doesn't need a predicate if you don't need to filter the objects.
same:all cached query method;
difference:
Fetch Requests-->really Fetch Requests;just fetch something from a single table
Fetched Properties --> fetch something from different table based on data from this table;like use :select from table2 where id = thisrow.id

Create NSFetchedResultsController managing entities with many to many relationship

I have an entity called Project and another entity called Employee. Employees work on multiple projects.
Project entity has project name.
Employee entity has First name, last name, departmentid number.
I want the data to show up in section header table like this
Project 1
Dept1
-firstname1, lastname1
-firstname2,lastname2
dept 2
firstname3, lastname3
firstname4,lastname4
Project 2
Dept1
-firstname1,lastname1
How can I do this? I don't have to display department names, but it has to be sorted that way.
I am using Core Data & UITableView. I need to construct NSFetchResultsController for this.
I think the root of your question comes from the fact that a to-many relationship results in an NSSet when you access it from the from object of the relationship (ie: Project->Employees - results in an NSSet of employees). NSSets are, of course, unordered.
And the answer is this:
You'll need to sort your employee NSSets by hand. I suggest you convert the NSSet to an NSMutableArray, and then use -sortUsingBlock: or something along those lines to do it. To keep yourself from having to re-sort it every time you need it, store it as a member variable of your Project class. In doing so, it should be pretty easy to create it lazily, and only re-sort it when the dataset changes, which will be better for performance.
You MIGHT be able to do something with a subquery in Core Data... but I think you might find that'll hit the disk more often than you might like. (Just a guess there) The technique I've suggested above is a bit less magical, a bit more brute force, but it'll work, and you'll know exactly how it behaves forever.
Use fetchedResultsController to get your 'Project' entities and you will be able to access and display the Employee NSSet in your tableview datasource methods via their relationships.

Resources