KarlJay
KarlJay

Reputation: 79

returning parent and child from coredata not working

I have 2 tables in coredata:

@interface Section (CoreDataProperties)

@property (nullable, nonatomic, retain) NSString *button;
@property (nullable, nonatomic, retain) NSString *header;
@property (nullable, nonatomic, retain) NSString *position;
@property (nullable, nonatomic, retain) NSString *projectid;
@property (nullable, nonatomic, retain) NSNumber *scroll;
@property (nullable, nonatomic, retain) NSString *source;
@property (nullable, nonatomic, retain) NSString *status;
@property (nullable, nonatomic, retain) NSString *subheader;
@property (nullable, nonatomic, retain) NSString *tag;
@property (nullable, nonatomic, retain) NSString *userid;
@property (nullable, nonatomic, retain) NSSet<Cell *> *cells;

@end

@interface Section (CoreDataGeneratedAccessors)

- (void)addCellsObject:(Cell *)value;
- (void)removeCellsObject:(Cell *)value;
- (void)addCells:(NSSet<Cell *> *)values with:(NSDictionary *) inputData;
- (void)removeCells:(NSSet<Cell *> *)values;

@end

@interface Cell (CoreDataProperties)

@property (nullable, nonatomic, retain) NSString *cellid;
@property (nullable, nonatomic, retain) NSString *overpic;
@property (nullable, nonatomic, retain) NSString *pictitle;
@property (nullable, nonatomic, retain) NSString *position;
@property (nullable, nonatomic, retain) NSString *subpic;
@property (nullable, nonatomic, retain) NSString *subtitle;
@property (nullable, nonatomic, retain) NSString *title;
@property (nullable, nonatomic, retain) Section *section;

@end

A custom save for the child record:

- (void)addCells:(NSSet<Cell *> *)values with:(NSDictionary *) inputData{
    [values setValue:[inputData objectForKey:@"overpic"] forKey:@"overpic"];
    [values setValue:[inputData objectForKey:@"pictitle"] forKey:@"pictitle"];
    [values setValue:[inputData objectForKey:@"position"] forKey:@"position"];
    [values setValue:[inputData objectForKey:@"subpic"] forKey:@"subpic"];
    [values setValue:[inputData objectForKey:@"subtitle"] forKey:@"subtitle"];
    [values setValue:[inputData objectForKey:@"title"] forKey:@"title"];
}

The custom save is called here:

        [tempSection addCells:(NSSet *)[Cell MR_createEntity] with:myDictionary];

I save with:

        [localContext MR_saveToPersistentStoreAndWait];
localContext is at the top of the this method:
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];

I then get the records with:

NSArray *sections = [Section MR_findAll];

List out the context with:

for (int j=0; j<sections.count; j++) {
    mySection = [sections objectAtIndex:j];
    MLog(@"sections header %@",mySection.header);
    MLog(@"sections button %@",mySection.button);
    MLog(@"sections position %@",mySection.position);
    MLog(@"sections projectid %@",mySection.projectid);
    MLog(@"sections scroll %@",mySection.scroll);
    MLog(@"sections source %@",mySection.source);
    MLog(@"sections status %@",mySection.status);
    MLog(@"sections subheader %@",mySection.subheader);
    MLog(@"sections tag %@",mySection.tag);
    MLog(@"sections userid %@",mySection.userid);
    MLog(@"cell count: %lu", mySection.cells.count);
}

The problem is the cell count is 0. (actual output of mySection.cells is below)

I list out the cells in another fetch and the cells are there.

The relation is one section to many cells.

I noticed that in CoreData you don't set a connecting field, so I assume that CoreData takes care of connecting the child to the parent.

I'm using MagicalRecord and see nothing in the docs to help with this.

I'm able to fetch all the records from either table, but there is no connection between them. I don't know if a custom adding of the child fields is the problem, but I didn't know how to add the NSSet field in the child record or if I'm supposed to do that.

If I grab just one Section with this:

Section *onlyOne = [Section MR_findFirstByAttribute:@"position" withValue:@"02" inContext:localContext];

MLog(@"section cells %@",onlyOne.cells);

I get this:

 section cells Relationship 'cells' on managed object (0x7f8c1af282c0) <Section: 0x7f8c1af282c0> (entity: Section; id: 0xd000000000740000 <x-coredata://2084C475-532F-4BBB-86A3-FFFBE63A0BB3/Section/p29> ; data: {
    button = Change;
    cells =     (
    );
    header = "Tech News";
    position = 02;
    projectid = "";
    scroll = 1;
    source = "source here";
    status = 2;
    subheader = "subHeader Here for Tech News";
    tag = 2;
    userid = nil;
}) with objects {(
)}

^^ this has the Section data and a "with objects" that looks empty instead of having the cells that belong to that Section.

How do I get the cells connected to their sections ?

Upvotes: 0

Views: 44

Answers (1)

Brett Donald
Brett Donald

Reputation: 14277

In addCellsObject:, your code should look like this:

- (void)addCellsObject:(Cell *)value
{
    NSMutableSet* set = [self mutableSetValueForKey:@"cells"];
    [set addObject:value];
}

So you call the method with an existing Cell, and the method adds the Cell to the cells set (property of the Section). Not sure what you are trying to achieve in your other method, but you must at least add cells to the cells set.

Upvotes: 2

Related Questions