I have to say that everyone on the forum has been really helpful with my attempts at learning core data.
I am adding attribute values to my core data entities and creating a relationship when the user selects a row as shown below:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
UIAlertView *messageAlert = [[UIAlertView alloc]
initWithTitle:@"Row Selected" message:@"Added to Routine!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
NSManagedObjectContext *context = [self managedObjectContext];
// Create a new device
ExcerciseInfo *info = [_fetchedResultsController objectAtIndexPath:indexPath];
NSManagedObject *routineEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Routines"inManagedObjectContext:context];
NSManagedObject *routineEntityDetail = [NSEntityDescription insertNewObjectForEntityForName:@"RoutinesDetails" inManagedObjectContext:context];
[routineEntityDetail setValue:routineEntity forKey:@"routineinfo"];
[routineEntity setValue: RoutineText forKey:@"routinename"];
[routineEntityDetail setValue: info.details.muscle forKey:@"image"];
How would I include an IF statement
whereby if the routinename
already exists the new entry would be added to the existing relationship?
Is this easily possible from the current code? So the Test Routines will be grouped instead of showing separately and the detail view would include both entries. Would NSPredicate
be appropriate here? Or perhaps the use of distinctUnionOfObjects
?
You thoughts and comments will be appreciated.
AS REQUESTED -
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Routines" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"routinename" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext sectionNameKeyPath:nil
cacheName: nil];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;