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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…