gpt4 book ai didi

ios - 从 coredata 返回 parent 和 child 不工作

转载 作者:行者123 更新时间:2023-11-28 21:27:25 24 4
gpt4 key购买 nike

我在 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 的实际输出如下)

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

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

我注意到在 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 {(
)}

^^ 这具有部分数据和一个看起来是空的“带有对象”,而不是具有属于该部分的单元格。

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

最佳答案

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

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

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

关于ios - 从 coredata 返回 parent 和 child 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37559339/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com