Custom ordering Core Data to-many relationship? - core-data

How can I sorting by a 'custom' order managed object in a to-many relationship? It seems managed objects are in an arbitrary order, and I need them ordered alphabetically by name property. Shall I extend managed object / entity?

Yes, managed objects in Core Data are in no particular order. You can order them when you fetch by setting the sortDescriptors property on the fetch request. If you are already doing a fetch, that is faster and more efficient than fetching first and then sorting in memory, for example, by calling sortedArrayUsingDescriptors.
So, if you were to fetch all Events, you would do as follows:
let fetchRequest: NSFetchRequest<Event> = Event.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(key: #keyPath(Event.name),
ascending: true)]
Or, if you have a many-to-many relationship from Events to Users that is called usersWhoLiked, you could fetch all events that were liked by at least one user over age 50 as follows:
let fetchRequest: NSFetchRequest<Event> = Event.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "#"SUBQUERY(usersWhoLiked, $user, $user.age > %d).#count != 0",
50))
fetchRequest.sortDescriptors = [NSSortDescriptor(key: #keyPath(Event.name),
ascending: true)]
But, note that the sortDescriptors apply ONLY to the fetch request's entity. And, if you are not doing a fetch at all, for example just ordering all the events of a particular user, then Jon Rose's solution may be just as efficient or more.
It's unclear what exactly you want to order. I hope this answers your question.

The relationship is defined as a set, which is unordered. You can turn the set into an array and then sort the array:
NSArray* sortedEvents = [user.events.allObjects sortedArrayUsingDescriptors:#[[NSSortDescriptor sortDescriptorWithKey:#"name" ascending:YES]]]

Related

How to configure an NSFetchRequest to retrieve a to-many relationship

I've got a Core Data relationship set up like this:
Place PlaceList
===== =========
lists <<--------->> places
I'd like to populate an NSFetchResultsController with a given PlaceList instance's list of places. places is an ordered relationship, and lists is unordered. I'd like the fetch request to return the list of places in the order specified by the relationship.
I started out with this:
let request = NSFetchRequest<Place>(entityName: "Place")
request.predicate = NSPredicate(format: "lists == %#", placeList)
request.sortDescriptors = [NSSortDescriptor(key: "lists", ascending: true)]
When I fetch, though, no places come back. I've tried a bunch of ideas from other answers, trying all different predicates, and most result in exceptions thrown when the fetch request is executed.
I've tried, for example:
"ANY lists == %#"
"lists CONTAINS %#"
"%# IN lists"

NSFetchRequest to-many key not allowed here

I have a many to many relationship between two entities; A and B.
I want to return an array of B.relationship for every A where B.relationship’s count is greater than 0 and sorted by B’s dateCreated property.
This is the code I currently have which probably makes a little more sense.
let fetchRecentVariationsRequest = NSFetchRequest(entityName: "Variation")
fetchRecentVariationsRequest.predicate = NSPredicate(format: "ANY activities.#count > 0")
fetchRecentVariationsRequest.sortDescriptors = [NSSortDescriptor(key: "activities.dateCreated", ascending: true)]
When I run the request I get the following exception:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here’
I understand why I’m getting the error but how I sort by a to many property for a sort descriptor in Core Data?
Edit
To be more clear, I’d like to fetch the 6 most recent Activity entities sorted by their dateCreated property (newest first).
Then I’d like to fetch all of the Variation entities which are related to these fetched Activity entities via the Activity entity’s variations relationship.
You can't sort by the attribute of a to-many relationship, because it makes no sense. CoreData needs to decide which Variation to put first. Your sort descriptor says "use the value of dateCreated on the related Activities". But there are several Activities and so several different dateCreated values for each Variation. Which Activities' dateCreated should it use? The last? The first? The average?
But over and above that conceptual problem, CoreData will only allow you to use an attribute, or a to-one relationship, to sort by (at least for a fetch from the SQLite store). No transient properties; no computed properties. So if you want to use the dateCreated of the most recent related Activity, you will need to add an attribute to Variation which you update every time an Activity is added to the relationship.
EDIT
Given your update, I would fetch the most recent six Activities first:
fetchRecentActivitiesRequest = NSFetchRequest(entityName: "Activity")
fetchRecentActivitiesRequest.sortDescriptors = [NSSortDescriptor(key: "dateCreated", ascending: false)]
fetchRecentActivitiesRequest.fetchLimit = 6
// I recommend using the next line to fetch the related Variations in one go
fetchRecentActivitiesRequest.relationshipKeyPathsForPrefetching = ["variations"]
let recentActivities = try! context.executeFetchRequest(fetchRecentActivitiesRequest) as! [Activity]
and then use the variations relationship to get the corresponding Variations:
let nameSort = NSSortDescriptor(key: "name", ascending: true)
let recentVariations = recentActivities.flatMap() {
$0.variations!.sortedArrayUsingDescriptors([nameSort])
}

Sorting NSFetchedResultsController using a to-many relationship property

SCENARIO
I have two entities: Item and ListDetail (which contains prices for different lists for every item). This is absolutely needed and I can't provide a price attribute for the Item entity because every item can have more prices for different dynamic lists (retail, b2b ecc.).
The relationship is:
Item (lists) <------->> (item) ListDetail
The current active list in my app change dinamically, so let's say I have an integer variable with the current active list: _ACTIVE_LIST_CODE_. When I need a price for an item object I use an helper method on the Item class:
-(NSNumber*) getPrice {
NSSet *lists=[self.lists filteredSetUsingPredicate: [NSPredicate predicateWithFormat:#"listId == %d",_ACTIVE_LIST_CODE_]];
ListDetail *activeList=[[lists allObjects] objectAtIndex:0];
return activeList.price;
}
THE PROBLEM
I use a UITableView with NSFetchedResultController in order to select and show some items for different sections. Nothing special. I would like to order the fetchedObjects using the items price for the active list. If price was an attribute of Item I would added simply a sort descriptor to the fetch request like so:
[NSSortDescriptor sortDescriptorWithKey:#"price" ascending:YES];
But as said before this is not possible, price is a dynamic attribute.
If using transient properties was possible for sort descriptors, I would set a price transient properties calculated on fly using my helper method. Nothing to do.
Using a keypath in the descriptor like "lists.price" is not possible (or maybe I don't know how to do that), just because it's a to-many relationship and it's modeled with a NSSet.
I tried some workaround, without success:
1) observing _ACTIVE_LIST_CODE_ changes to set items price in a non-transient attribute.
2) after the fetch request, before presenting the table view, reorder a brand new array with fetched objects using the transient "price" property, iterate the orderdered array following an ascending integer index "i" and assigning this value to a non-transient property "order" for the Item entity. Using "order" for sort descriptor in the fetch request. (This approach is described here: Re-ordering NSFetchedResultsController)
Both of them works, but they slow down performance because I have thousands of items in the fetch results... Any idea?
How about fetching ListDetail instead? You could restrict and sort with the appropriate predicates and sort descriptors, exactly as you propose.
fetchRequest.predicate =
[NSPredicate predicateWithFormat:#"listID = %#", activeListCode];
fetchRequest.sortDescriptors =
#[[NSSortDescriptor sortDescriptorWithKey:#"price" ascending:YES]];
Now, to group by some attribute of item should be simple and efficient because it is a to-one relationship. Your fetched results controller's sectionNameKeyPath can be something like
#"item.category"

Filtering a relationship of an NSManagedObject

Suppose a Manager has a to-many relationship with Employee objects. Given a reference to a Manager object (i.e. NSManagedObject *manager), how do I get a reference to "the Employee with the lowest salary among all of those whose salaries exceed 10000"?
I can think of two possible approaches:
Approach 1: constructing an NSFetchRequest and specifying the Manager wanted with the object ID of the Manager in question.
Approach 2: some kind of key-value coding expression on Manager, e.g. [manager valueForKeyPath:#"..."] (with some use of NSPredicate?)
I'm inclined towards Approach 2 if there's a plausible solution. Please enlighten me.
Of course, you can just apply a predicate and sort descriptor to the set returned by the relationship. Easy, and pretty quick if the set is relatively small (because it is done in memory, all the objects will have to be fetched). You may want to do that batch fetch up front to limit the number of times you do I/O.
Depending on the size of the database and the number of employees under the manager (and the indexing), you may want to do it all at the database level...
// We want "Employee" objects
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"Employee"];
// Assuming the "inverse" relationship from employee-to-manager is "manager"...
// We want all employees that have "our" manager, and a salary > 10000
fetchRequest.predicate = [NSPredicate predicateWithFormat:#"(manager == %#) AND (salary > 10000", manager];
// Sort all the matches by salary, little-to-big
fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:#"salary" ascending:YES]];
// Limit the returned set to 1 object, which will be the smallest
fetchRequest.fetchLimit = 1;
This will perform the entire query at the database level, and only return 1 object.
As always, performance issues are usually highly dependent on your model layout, and the options used for specifying the model and its relationships.
You can filter your array of Employee relationship to get the one you want.
1) First, get all the Employee with salaries over 10000:
NSArray *filtered = [manager.employees filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(salary > 10000)"]];
2)Then sort it in descending order
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: #"salary" ascending: NO];
NSArray *sortedAndFiltered = [filtered sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];
3)Then just get your employee
[sortedAndFiltered lastObject];

What is the correct way for quering a NSNumber in a NSArray within CoreData?

I have a Entity with a column of type ID named "responsibleUsers". In this column I store an Array containing NSNumbers.
I want to fetch all objects of this entity, that match my current User. Therefore i create following predicate:
[NSPredicate predicateWithFormat: #"%# IN responsibleUsers",[NSNumber numberWithInteger: curUser.oID.integerValue] ]
whatever I try, my App crashes. Once with a EXC_BAD_ACESS, once with "unimplemented SQL generation for predicate nsnumber"
What is the correct way to query my entity?
The query you are trying assumes that you have two entities: the entity you querying (let's call it Group) and another one, perhaps called User, which is set up as a to-many relationship from Group called responsibleUsers. You would then be able to use the predicate you suggest:
[NSPredicate predicateWithFormat:#"%# IN responsibleUsers, aUser];
This would be the recommended use of Core Data object graphs.
In your setup, it seems you have an array of NSNumber set as a property rather than a relationship. Therefore you cannot use such a query. You simply have to retrieve the array and query the array.
BOOL containsResponsibleUser = NO;
for (NSNumber *n in aGroup.responsibleUsers) {
if ([n isEqualTo:[NSNumber numberWithInteger: curUser.oID.integerValue]])
containsResponsibleUser = YES;
}
If you are indeed querying something like a group of users, I would recommend the first approach. If you are querying some kind of user, I would suggest a BOOL property responsible as the most efficient solution.

Resources