2
votes

I have a one-to-many CoreData model. There are tasklists and tasks belonging to the tasklists. My problem is that whenever I create a new task, a tasklist is also created.

"new tasklist" is the default value of the name field in the db. "New tasklist" is the value stored by the View when it creates a new tasklsit. As can be seen, every time a task is created, a tasklist with the default name value is automatically created.

Question Is this how it is supposed to look? If so, how do I do a query to pick only the tasklist names where Z_ENT = 1 (those are the parents, right?) If this looks very wrong, how do I insert stuff correctly. I think my table relationships are correct - but if that might be the problem, I ll post screenshots.

Model Diagram enter image description here

My Code

When a list is selected:

    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    TaskViewController *taskViewController = [[TaskViewController alloc] init];
    taskViewController.managedObjectContext = [self.fetchedResultsController managedObjectContext];
    taskViewController.tasklist = managedObject;

In the TaskViewController, I get the fetchedResultsController like this:

    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                              @"(tasklistOfTask == %@)", tasklist];
    [fetchRequest setPredicate: predicate];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

and then to insert,

   NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
    NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

    [newManagedObject setValue:@"dummy" forKey:@"task"]; 
    [newManagedObject setValue:tasklist forKey:@"tasklistOfTask"];

the managedobjectcontext is passed in from AppDelegate.

1
Could you just show me a screenshot of your model diagram to know that how you connected relationships? - Dinesh Raja
Added the model diagram. Thanks! - crazyphoton
Looking at the graph, the bottom arrow looks correct, to-one/to-many. The arrow above it looks suspicious and the likely culprit -- although I've never seen it and am probably using an older version of Xcode. Are your relationships set up as the inverse as well as the destination of each other? Inverses are pretty much required. - Wienke
yup that was the problem.. thanks! - crazyphoton

1 Answers

1
votes

You've created an inheritance relationship between Tasks and Tasklists so every task is also a task list, and both entities are stored in the same table. I would recommend deleting this and changing your model so it looks like

this

Generate the managed object classes from your model if you're not already, and then your code would be something like:

   NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    TaskViewController *taskViewController = [[TaskViewController alloc] init];
    taskViewController.managedObjectContext = [self.fetchedResultsController managedObjectContext];
    taskViewController.tasklist = managedObject;

    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                              @"(tasklist == %@)", tasklist];
    [fetchRequest setPredicate: predicate];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

   NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
    Task *newTask = (Task *)[NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
    newTask.taskName = @"dummy";
    newTask.number = [NSNumber numberWithInteger:[tasklist.tasks count] + 1];
    newTask.tasklist = tasklist;