I have a simple directory manager function with items in categories EXCEPT a single item can be in several categories.
The item has a parent key, which is the NSSet of the parent categories
The category has a "items" key, which is NSOrderedSet for its subelements
I am using NSFetchedResultController and its delegate to populate my table with elements
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:self.managedObjectContext]; [fetchRequest setEntity:entity]; fetchRequest.predicate = [NSPredicate predicateWithFormat:@"(ANY parents == %@)", self.category]; [fetchRequest setFetchBatchSize:30]; [fetchRequest setSortDescriptors:@[????????]; NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil]; aFetchedResultsController.delegate = self; self.fetchedResultsController = aFetchedResultsController; NSError *error = nil; if (![self.fetchedResultsController performFetch:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); }
Therefore, using this code, I can get a list of elements in self.category
My user interface has Drag and Drop functionality. Therefore, I can move elements within categories.
My question: In categories 1 and 2, there are links to the same elements as Item1 and Item2. I need to have Item1, Item2 order in Class1 and Item2, Item1 order in Category2. So I made the property category.items as NSOrderedSet .
But I just can't sort the items by their index in category.items
I tried to use blocks in sorting descriptors - it did not work, I tried to use a selector in sorting descriptors - did not work, I tried to describe class descriptors - it works somehow, but does not update the elements when I change their index in a category. Like this in my NSSortDescriptorSubclass:
- (NSComparisonResult)compareObject:(Item*)object1 toObject:(Item*)object2 {
int index1 = [self.category.items indexOfObject:object1]; int index2 = [self.category.items indexOfObject:object2]; if (index1 > index2) { return NSOrderedDescending; } else if (index1 < index2) { return NSOrderedAscending; } return NSOrderedSame;
} Code> But the order of the elements will not be updated in my user interface if I change the order in Category.items
So please help me sort the items by their index in the category. Perhaps there is a way to do this through some keys, operators, expressions, something else. Thanks.