从coredata返回父母和孩子不起作用
Posted
技术标签:
【中文标题】从coredata返回父母和孩子不起作用【英文标题】:returning parent and child from coredata not working 【发布时间】:2016-06-01 04:17:28 【问题描述】:我在 coredata 中有 2 个表:
@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 的实际输出如下)
我在另一个 fetch 中列出了单元格,并且这些单元格就在那里。
关系是一个部分对多个单元格。
我注意到在 CoreData 中您没有设置连接字段,因此我假设 CoreData 负责将子级连接到父级。
我正在使用 MagicalRecord,但在文档中看不到任何帮助。
我能够从任一表中获取所有记录,但它们之间没有任何联系。我不知道自定义添加子字段是否是问题,但我不知道如何在子记录中添加 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 (
)
^^ 这具有部分数据和一个看起来为空的“带有对象”,而不是具有属于该部分的单元格。
如何将单元格连接到它们的部分?
【问题讨论】:
【参考方案1】:在 addCellsObject: 中,您的代码应如下所示:
- (void)addCellsObject:(Cell *)value
NSMutableSet* set = [self mutableSetValueForKey:@"cells"];
[set addObject:value];
因此,您使用现有 Cell 调用该方法,该方法将 Cell 添加到单元集(Section 的属性)中。不确定您要在其他方法中实现什么,但您至少必须将单元格添加到单元格集中。
【讨论】:
以上是关于从coredata返回父母和孩子不起作用的主要内容,如果未能解决你的问题,请参考以下文章