Note that there are some explanatory texts on larger screens.

plurals
  1. PONSFetchedResultsController and NSOrderedSet relationships
    primarykey
    data
    text
    <p>I am having an issue (understanding issue to be honest) with NSFetchedResultsController and the new NSOrderedSet relationships available in iOS 5.</p> <p>I have the following data-model (ok, my real one is not drawer's and sock's!) but this serves as a simple example:</p> <p><img src="https://i.stack.imgur.com/dWE9y.png" alt="enter image description here"></p> <p>Drawer and Sock are both NSManagedObjects in a Core Data model/store. On <code>Drawer</code> the <code>socks</code> relationship is a <strong>ordered to-many</strong> relationship to <code>Sock</code>. The idea being that the socks are in the drawer in a specific order. On <code>Sock</code> the <code>drawer</code> relationship is the inverse of the <code>socks</code> relationship.</p> <p>In a UIViewController I am drawing a UITableView based on these entities. I am feeding the table using a <code>NSFetchedResultsController</code>. </p> <pre><code>- (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; } </code></pre> <p>When I run this, I get the following errror: <code>*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'</code></p> <p>This makes sense to me, as the relationship is a <code>NSOrderedSet</code> and not a single entity to compare against for sorting purposes.</p> <p>What I want to achieve is for the <code>Socks</code> to appear in the <code>UITableView</code> in the order specified in the <code>socks</code> relationship. I don't really want to have a sort order but <code>NSFetchedResultsController</code>, 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. </p> <p><strong>Note:</strong> I am using this within an iOS5 application only, so ordered relationships are available.</p> <p>Anyone that can offer me any direction, would be greatly appreciated. Thanks for your time.</p> <p><strong>Edit:</strong> 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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload