我不得不說,論壇上的每個人對我學習核心數據的嘗試都非常有幫助。將屬性值添加到核心數據中IF值已存在
我添加的屬性值來我的核心數據實體和當用戶選擇一個行創建的關係如下圖所示:
- (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;
你可以分享你的'ManagedObjectModel'嗎? 'RoutineDetails'與'RoutineEntity'是一對多的關係嗎? –
@MarkoNikolovski我已經更新了這個問題。是的,這些實體處於一對多的關係。我已經包含了實體的快照。謝謝 – Sgillon