1
votes

My Core Data app has two entities: "Note" and "Marker". The Note entity has a 1-to-many relationship to markers (ie. A note contains many markers). I have a fetchedRequestController which is responsible for fetching all the "Note" entities. After creating 1 note and 1 marker (which belongs to that note) I get an error because the fetchedRequestController fetches both the Marker and Note. The Note is expected but the Marker should not be fetched.

Here is my fetched request controller

    TCModel *model = [TCModel sharedModel];
    NSManagedObjectContext *context = [model managedObjectContext];
    NSParameterAssert(context);
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Note" inManagedObjectContext:context];
    NSParameterAssert(entity);
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    NSFetchedResultsController *controller = [[NSFetchedResultsController alloc]
                                              initWithFetchRequest:fetchRequest
                                              managedObjectContext:context
                                              sectionNameKeyPath:nil
                                              cacheName:@"Root"];
    NSParameterAssert(controller);
    self.fetchedResultsController = controller;
    controller.delegate = self;
    NSError *error;
    BOOL success = [controller performFetch:&error];
    if ( success == NO )
    {
        NSLog(@"Failed to fetch!");
        NSParameterAssert(nil);
    }

The objects are created using two helper methods and then saved using a third

- (TCNote *)newNote
{
    TCNote *note = [NSEntityDescription insertNewObjectForEntityForName:@"Note"
                                                 inManagedObjectContext:self.managedObjectContext];
    note.creationDate = [NSDate new];
    return note;
}

- (TCMarker *)newMarker
{
    TCMarker *marker = [NSEntityDescription insertNewObjectForEntityForName:@"Marker"
                                                 inManagedObjectContext:self.managedObjectContext];
    return marker;
}

- (void)_save
{
    NSError *error;
    NSLog(@"Saving");
    if (![self.managedObjectContext save:&error])
    {
        NSLog(@"Error saving context: Error = %@", error);
    }
}

Here is the console output:

2013-01-03 17:41:12.062 timecode[10269:c07] CoreData: sql: SELECT Z_VERSION, Z_UUID, Z_PLIST FROM Z_METADATA

2013-01-03 17:41:12.063 timecode[10269:c07] CoreData: sql: SELECT t0.Z_ENT, t0.Z_PK, t0.Z_OPT, t0.ZNOTEDATE, t0.ZSCENEDESCRIPTION, t0.ZSCENETITLE, t0.ZTIMECODEDATE, t0.ZDATE, t0.ZTITLE, t0.ZPARENT, t0.Z1_PARENT FROM ZNOTE t0

2013-01-03 17:41:12.064 timecode[10269:c07] CoreData: annotation: sql connection fetch time: 0.0005s

2013-01-03 17:41:12.064 timecode[10269:c07] CoreData: annotation: total fetch execution time: 0.0011s for 2 rows.

1
Updated & approved answers. Wasn't aware of that before ;)bluefloyd8
Just noticed that my SQLite schema doesnt have a table for the Marker entities. Not sure why that would be the case. There is a ZNOTE table but not a ZMARKER one.bluefloyd8

1 Answers

1
votes

You have defined Note as "parent entity" of Marker, i.e. Marker is a "child entity" of Note:

enter image description here

That means that every Marker object is also a Note object (same as with class and subclass). Fetching all objects of the Note entity does therefore also return the objects of the Marker entity.

The SQLite file contains in this case only one table ZNOTE, which has columns for the properties of Note and for the additional properties of Marker.

So if you don't really need Marker to be a child entity of Note, just set the "Parent Entity" to "No Parent Entity".