I am having an issue (understanding issue to be honest) with NSFetchedResultsController and the new NSOrderedSet relationships available in iOS 5.
I have the following data-model (ok, my real one is not drawer's and sock's!) but this serves as a simple example:
Drawer and Sock are both NSManagedObjects in a Core Data model/store. On Drawer
the socks
relationship is a ordered to-many relationship to Sock
. The idea being that the socks are in the drawer in a specific order. On Sock
the drawer
relationship is the inverse of the socks
relationship.
In a UIViewController I am drawing a UITableView based on these entities. I am feeding the table using a NSFetchedResultsController
.
- (NSFetchedResultsController *)fetchedResultsController1 {
if (_fetchedResultsController1 != nil) {
return _fetchedResultsController1;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Sock" inManagedObjectContext:[NSManagedObjectContext MR_defaultContext]];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"drawer.socks" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
self.fetchedResultsController1 = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[NSManagedObjectContext MR_defaultContext] sectionNameKeyPath:nil cacheName:@"SocksCache"];
self.fetchedResultsController1.delegate = self;
return _fetchedResultsController1;
}
When I run this, I get the following errror: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'
This makes sense to me, as the relationship is a NSOrderedSet
and not a single entity to compare against for sorting purposes.
What I want to achieve is for the Socks
to appear in the UITableView
in the order specified in the socks
relationship. I don't really want to have a sort order but NSFetchedResultsController
, which is a great component is insisting there has to be one. How can I tell it to use the socks order on the Drawer entity. I don't want the table to show Drawer entities at all.
Note: I am using this within an iOS5 application only, so ordered relationships are available.
Anyone that can offer me any direction, would be greatly appreciated. Thanks for your time.
Edit: So the tableview that display's socks does so for just one drawer. I just want the table view to honor the order that the socks relationship contains. I'm not sure what to set the sort criteria to make sure that happens.
socks
relationship" - how do you go about specifying the order of the relationship? In the editor I can just see a check-box (ordered or not ordered), but nothing to control the sort criteria. – TerinainsertObject:inSocksAtIndex:
method, but I get an unrecognized selector exception. I thought the managed object context would add this at run-time? – TerinaNSFetchRequest
to getsocks
with a[NSPredicate predicateWithFormat:@"drawer == %@", theDrawer]
and my own sort descriptor on the same request. This way I can sort my socks bysize
orcolor
in different circumstances and sync them with iCloud. – Terina