I have a parent in my Event model. And two children: Birthday, Anniversary. I use the object inheritance function built into Core data, so the parent of the birth and anniversary is an event.
So, I am making a selection using the following:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext]; [fetchRequest setEntity:entity];
Now I want to sort fetchedObjects by event type, birthday or anniversary.
How can I do it? I tried:
for (Birthday *birthday in _fetchedResultsController.fetchedObjects){ [birthdayObjects addObject:birthday]; } for (Anniversary *anniversary in _fetchedResultsController.fetchedObjects){ [anniversaryObjects addObject:anniversary]; }
But it just adds all the objects in fetchedObjects to each array.
Any ideas, or am I not mistaken about this?
UPDATE :
It turned out using this:
for (Event *event in _fetchedResultsController.fetchedObjects){ if ([event isKindOfClass:[Birthday class]]){ [birthdayObjects addObject:event]; }else{ [anniversary addObject:event]; } }
But if there is a better way, I am open to this! Thank you
source share