我不得不说,论坛上的每个人都对我学习核心数据的尝试非常有帮助。
我正在向我的核心数据实体添加属性值,并在用户选择一行时创建关系,如下所示:
- (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"];
IF statement
如果已经存在,我将如何包含一个routinename
新条目将添加到现有关系中?
这很容易从当前代码中实现吗?因此,测试例程将被分组而不是单独显示,并且详细视图将包括这两个条目。NSPredicate
这里合适吗?或者也许使用distinctUnionOfObjects
?
您的想法和意见将不胜感激。
按照要求 -
- (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;