从 coredata 返回 parent 和 child 不工作

returning parent and child from coredata not working

我在 coredata 中有 2 tables:

@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

child 记录的自定义保存:

- (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"];
}

这里调用自定义保存:

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

我保存:

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

然后我通过以下方式获取记录:

NSArray *sections = [第 MR_findAll];

列出上下文:

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);
}

问题是单元格计数为0。(mySection.cells的实际输出如下)

我在另一个提取中列出了单元格,这些单元格就在那里。

关系是一个部分到多个单元格。

我注意到在 CoreData 中您没有设置连接字段,所以我假设 CoreData 负责将 child 连接到 parent。

我正在使用 MagicalRecord,但在文档中看不到任何帮助。

我可以从 table 中获取所有记录,但它们之间没有任何联系。我不知道 child 字段的自定义添加是否有问题,但我不知道如何在 child 记录中添加 NSSet 字段,或者我是否应该这样做.

如果我只抓住一个部分:

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

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

我明白了:

 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 {(
)}

^^ 这包含部分数据和一个 "with objects",它看起来是空的,而不是包含属于该部分的单元格。

如何将单元格连接到它们的部分?

在 addCellsObject: 中,您的代码应如下所示:

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

因此您使用现有 Cell 调用该方法,该方法将 Cell 添加到单元格集(属性 of the Section)。不确定您在其他方法中试图实现什么,但您至少必须将单元格添加到单元格集中。