У меня есть 2 таблицы в CoreData:возвращения родителей и ребенка из 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 * разделы = [Раздел 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 в дочернюю запись или если я должен это сделать.
Если я захватить только один раздел с этим:
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 {(
)}
^^ это имеет данные секцию и «с объектами», который выглядит пустым, вместо того, ячейки, принадлежащие этому разделу.
Как подключить ячейки к их разделам?