how to join tables with NSManagedObject - ios

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.

Related

Best way of track the order of entities stored in CoreData

I am creating this App which can display some contents in a collection view. The data source of the collection view is stored locally by using CoreData. Each collection view cell will have a corresponding CoreData entity.
Since I am adding a "drag to reorder" feature to the collection view, I need some way to track the order of the CoreData entities. Anyone have any suggestions of how to achieve that?
Appreciate your help!
You may add an integer property to your entity type. Every time you change the order of your entities you assign the current index to this property and save the context after that. You can fetch the entities sorted by this property to restore the order.
But this solution may have two problems:
It may be not performant for large entity sets, and/or frequent updates.
The entity order may not belong to the entity content. This can be problematic, for example, if the data is to be displayed in different views in different order.
In that case you should store the order not with the entities. Instead you can store the order in a property list, e.g. in an array with the identifiers of 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).

Core Data:Fetch rlelationships or Fetch Main Entity

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.

Creating NSFetchedResultsController sections based on type of relationship between entities

In an app I'm developing, I have a Core Data model which has Users and Widgets where a user can own many Widgets as their owner or watch Widgets as one of many watchers. For a screen in my app, I want to display a table view with two sections: the first lists Widgets a given User owns, while the other shows the Widgets that User is watching.
So basically what this boils down to is basing sections on whether a given Widget is related to the User by the watchers or owner relation. Is there a simple way to achieve this?
You can have two relationships between Users and Widgets. The User Entity would have ownedWidgets and watchedWidgets, whilst the Widget Entity would have owner and watchers.
User Widget
------ --------
ownedWidgets <----->> owner
watchedWidgets <<-->> watchers
Edit
Didn't notice the NSFetchedResultsController in the question title! The problem you've got is that the sectionNameKeyPath property is used by the FRC to determine sections based on one attribute. You've already done this work with the relationships, and you don't have one attribute to cover these two cases.
My suggestion would be to create two arrays from the specific User instance ownedWidgets and watchedWidgets, sorted with the same sort descriptor that you are using in your FRC, and use those for the Table View. When the FRC updates rows, you can recreate those arrays before calling [tableView endUpdates]. Saves having to alter your data model to accommodate one view controller design.
Frankly, if you already have the User object, you already have the Widgets you need via the relationships, so you don't really need to fetch anything. You might want to consider whether you really need the FRC at all.
And if you do need to use the FRC - Erik H.'s answer would be the way to go.
This depends on your data model. I think one way you could achieve this by using an intermediate object UserWidget and having an attribute (like relationshipType) for the type of relationship, owner or watcher. Then your fetch could be on UserWidgets and the sectionNameKeyPath could be relationshipType.

core data array as attribute

I want create attribute of "event" entity that will have a short list of events what the correct way to make it?
I think the right way is just use array but how can I do it? if someone can give me code example it will be nice.
Don't listen to any advice regarding foreign keys - they do not exist in Core Data. What you have to do is link your Event entity to another (or itself) with a relationship.
It is not clear why an event would have a short list of events. Maybe you want to distinguish event types or something similar. You could then create a new entity EventType and establish a to-many relationship in the Core Data Model Editor:
Event <<----->> EventType
Now an event could be linked an arbitrary number of EventType objects. You could use a relationship name like allowedEventTypes for each event and access this set (not an array, mind you, but an NSSet with unordered unique objects):
NSSet *types = event.allowedEventTypes;
Once you master the core data modeling technique, the coding becomes exceedingly simple.

Resources