从coredata返回父母和孩子不工作

时间:2016-06-01 04:17:28

标签: ios objective-c core-data magicalrecord

我在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

儿童记录的自定义保存:

- (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 = [Section 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负责将孩子连接到父母。

我使用MagicalRecord并在文档中看不到任何内容来帮助解决此问题。

我能够从任一表中获取所有记录,但它们之间没有连接。我不知道自定义添加子字段是否有问题,但我不知道如何在子记录中添加NSSet字段,或者我是否应该这样做。< / p>

如果我只抓住一个部分:

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

^^这有截面数据和&#34;对象&#34;看起来是空的,而不是拥有属于该部分的单元格。

如何将细胞连接到它们的部分?

1 个答案:

答案 0 :(得分:2)

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

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

因此,您使用现有的Cell调用该方法,并且该方法将Cell添加到单元格集(该部分的属性)。不确定您在其他方法中尝试实现的目标,但必须至少将单元格添加到单元格集中。

相关问题