Filter to-many related/child entities of NSManagedObject - cocoa

I have an entity called Playlist which has a to-many relation with Event entity called events.
I also have NSArrayController configured in Entity Mode for managing Playlist entities
I need to Pre Fetch and filter events based on a criteria. The question is where exactly should I fetch events?
a. In Playlist's awakeFromFetch
b. In Playlist contoller's fetchWithRequest
c. Or is there a way to pre-fetch related entities based on a filter criteria?

Related

CRM 2016 - How to create a parent-child relationship with multiple parent types?

I have an entity - EntityZ, which has a ParentId where ParentId could be EntityA.Id, EntityB.Id or EntityC.Id. Is it possible to create this in MS Dynamics CRM 2016? If yes, how? I've looked but couldn't find a similar question or any help on the web.
An entity can be the child party in only one full parental relationship. When you are looking for a way to cascade record ownership, deletion a.o. between mutiple parent - child entities you can create configurable cascading relationships.
As Arun Vinoth pointed out you can design your entity as an activity type. However, this may conflict with the semantics of activity records in CRM. Also, doing this would make it possible to associate the child entity to any entity that is enabled for activities.
There’s a trade off to achieve this. Custom entity as Custom Activity
Creating custom entity EntityZ as custom activity and EntityA, B, C can act as parent.
EntityA or B or C can be chosen as RegardingObjectId of EntityZ.
This has security limitation like EntityZ will be visible to everyone as this will be listed like other activities (Email, Phonecall, Task, etc)
I think it is worth nothing that Dynamics 365 does contain and out-of-box "polymorphic" customer field.
This field can link to either an Account or Contact:
And while it can be kludgy, another option would be to create 3 lookups and only populate one. Once one is populated you could hide the other two. Or, you could have a "Parent Type" option set to determine which lookup to show.
It would be a bit messy to show three lookups in a view, with only one populated, so you might also want a Parent Name text field in to which you could concatenate the type and name. You could use a workflow to populate it, and then use it in views and reports.

Binding an Ordered Relationship with an NSArrayController

How does one bind an NSArrayController's content to the entities in an ordered to-many relationship?
I have an unordered to-many relationship in my Core Data model, and an NSArrayController whose Content Set is bound to that relationship from the parent entity. This works fine, the data is accessible from the NSArrayController, no problem.
I decided during development that it would be better to allow users to manually reorder these child objects, so I changed the relationship to an ordered one. Now, when my NSArrayController is being created from my nib, the following error is presented:
Cannot create NSSet from object Relationship '...' fault on managed object ... of class _NSFaultingMutableOrderedSet
Now actually, I think this all makes sense: It's an ordered relationship, so now I'm getting an ordered set. Binding it to Content Array also would be inappropriate, since it's now an NSOrderedSet, not an array. My question is: Now how do I bind this relationship's data back into the NSArrayController?
I came across this discussion while searching to see if there've been any new developments on this front. In a shipping app I currently bind the array controller's content array to orderedSetKey.#array and it works just fine, not sure if I discovered that myself or if someone else suggested it somewhere.
The fundamental problem is that a Core Data ordered to-many relationship returns an NSOrderedSet, and NSOrderedSet is not a subclass of NSSet. Any array controller bindings that expect an NSSet will fail.
Tom Fewster has a detailed blog post describing the use of NSValueTransformer to work around this shortcoming, converting between NSOrderedSet and NSArray on the fly. He also provides a sample implementation on Github.

Adding to to-many / many-to-many core data relationship

I have 2 Entities, related by a many-to-many relationship.
Thing<<->>Tag
There is one NSArrayController controlling the entity "Tag", bound to the managedObjectContext. By the array controllers add: and remove: action i can add instances of tag to the collection.
There is a second NSArrayController controlling "Thing" entities, also bound to the managedObjectContext.
So each of the controllers manages all instances of their entity.
Now, let's say there are 5 "tag" and 3 "thing" instances already created by their array controllers.
I'd like to link individual tags to a thing. I just want to create the relationship between an existing thing to an existing tag instance.
Is addObject: of NSArrayController the right method for that? Or does it create a new managedObject?
Would it be equivalent to:
NSMutableSet *tags = [aThing mutableSetValueForKey:#"tags"];
[tags addObject:existingTag];
?
Is there some best practice for a tagging system?
I've found it helpful (in the latest version of Xcode) to select the entity in the core data modeller, and then go to the file menu, and select new file -> Core Data -> NSManagedObject subclass. It automatically creates a class with the necessary members AND ALSO methods for adding objects in the toMany relationships.
If you've done that, then you just need to get ahold of the thing instance to which you want to add a tag and you can call the method declared for you to do so. How that method is named is obvious from the header file generated.

Calling on a property of an entity's parent in a relationship

I have an NSColumnItem Selected Value bound to a relationship property (jobParent) in an entity (jobs). This is an inverse relationship to a property (projectChild) in a separate entity (projects).
How do I call on the projectTitle property of my projects entity to display in my NSTableColumn, based on the relationship to the currently selected jobTitle (from my jobs entity) in my NSTableView? I tried entering jobParent.projectTitle for the Model Key Path and arrangedObjects for the Controller Key but it's not working (as well as any other combination I can think of). Thanks.
The answer is to make sure the related entity is bound to 'content set' and then address the properties of the related table by using a period after the relationship property of the child entity followed by the property you want of the parent entity. For my example above, to get the projectTitle property of my projects entity in relation to my jobs entity, I would call on:
jobParent.projectTitle

What's the best way to model _ordered_ lists of items with core data (Mac OS X cocoa)?

What's the best way to model ordered lists of items with core data?
As of OS X 10.6, there is no automatic way to maintain an ordered to-many relation in Core Data. You will have to add an "index" attribute to the target of the to-many relation and maintain this index manually or maintain a mapping from object ID to index in the entity that has the ordered to-many relationship. It's actually not that hard to implement the first of these options by overriding the KVC orderd to-many accessor and setter methods. You can also create a fetched property on the parent entity to make (read-only) access to the ordered list easier by adding a sort descriptor to the fetched property's fetch predicate.
If you are targeting Lion or newer you can create ordered to-many relationships directly in the data modeler. Those use the NSOrderedSet class.

Resources