crazyphoton
crazyphoton

Reputation: 623

View contents of core data in emulator

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.

Upvotes: 2

Views: 410

Answers (1)

Simon Lawrence
Simon Lawrence

Reputation: 574

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;

Upvotes: 1

Related Questions