Is it possible to have Core Data sort the "many" part of a To-Many relationship during a fetch request?
Asked Answered
C

1

2

I'm using Core Data to cache a decent amount of information, and I have a To-Many relationship set up among my managed objects. Naturally I use an NSFetchRequest to fetch an array of the singular side of that relationship. However, I populate a UITableView using the the "many" side of the relationship, and I'd like it to be sorted alphabetically when I pull the data.

If I'm not being clear, here's an example:

"employee" and "boss" are both NSManagedObjects in a To-Many relationship - each boss has many employees, but employees only have one boss. After retrieving an array of bosses, I push a UITableViewController containing a list of employees. I'd like the employees to be pre-sorted during the NSFetchRequest to make displaying them easier. Is this possible, and how would I go about doing it?

Cryoscopy answered 23/10, 2010 at 11:6 Comment(1)
I haven't discovered any means of doing the sort, but creating a new sorted fetch of the data I need is pretty easy.Cryoscopy
W
2

Why not fetch from the reverse? I assume you already know the unique id of the boss you're interested in, so you could build a predicate like this one:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"boss.bossId == %K", bossId];
NSFetchRequest *req = [[NSFetchRequest alloc] init];
[req setEntity:employeeEntity];
[req setPredicate:pred];

That should fetch out any employee with a matching bossId, you can then add a sort descriptor to that fetch request to sort alphabetically.

Alternatively you could grab the array of NSManagedObject instances that you fetch out in your current implementation, and sort them using a sort descriptor (assumes your employees name is at the property "name"):

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YESorNO];
NSArray *sorted = [entities sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];

Now populate your tableview using "sorted".

Note: I'm a little confused about why you're not using an NSFetchedResultsController for this, as it would take care of sectioning for you as well.

Wakeful answered 25/10, 2010 at 19:11 Comment(5)
Maybe I'm not understanding you correctly, but I think that the first part of your answer is what I was talking about in the comment I added to my question.Cryoscopy
I think the second part of your answer would work, but I don't want to do any programatic sorting in the current view, since the app uses a prodigious amount of data and I don't want to take the time to sort data after it has been retrieved.Cryoscopy
As far as the NSFetchedResultsController goes, I'm new to Core data, so I hadn't looked into that when I posted the question! Thanks for the tip though - you're right, NSFetchedResultsController is an excellent tool. It's already implemented in one of my tableviews.Cryoscopy
Sorry I'll be a little more explicit. As I see it you have 2 options, reverse the target entity to employees (part 1 which you alluded to), or do some in memory sorting of the data. To my knowledge there is no other "simple" way to accomplish that sorting as sort descriptors apply to the target entity in the fetch.Wakeful
Yup, that's exactly the conclusion I arrived at. In my case the cleanest solution was a second fetch using the boss as the attribute to limit results by, since I was fetching results for another table, but in-memory sorting might work better for other situations.Cryoscopy

© 2022 - 2024 — McMap. All rights reserved.