多对多关系的核心数据 NSPredicate

发布于 2025-01-08 04:09:14 字数 977 浏览 0 评论 0原文

我在提出查询时遇到问题;这是场景:

my DB schema

基于此数据库架构,我知道命名部分有点糟糕,但这是 一个已经启动的项目,所以我必须坚持下去。

现在,我的目标是选择具有确定的 pack_id 和特定的category_id 的所有字符,即类别 5 中的 pack 1 中的所有字符,所以这就是我的 NSPredicate,

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(category_id == %@) AND (charRelationship.pack_id == %@)", [[cat valueForKey:@"category_id"] stringValue], curPack];
    [request setEntity:[NSEntityDescription entityForName:@"Category" inManagedObjectContext:self.managedObjectContext]];
    [request setPredicate:predicate];

    NSError *error;
    NSArray *result = [self.managedObjectContext executeFetchRequest:request error:&error];

一旦编译器尝试执行 fetchRequest,它就会崩溃并进入 SIGABRT 。 我真的很讨厌 xcode 甚至没有给我关于异常的线索,以便我可以自己弄清楚。因此,在盲目地尝试修复它但没有成功之后,我想知道是否有人可以帮助我。 我已经在 SO 和其他地方红了很多其他线程,但我找不到任何解决方案。

非常感谢

-k-

I've got a problem in the formulation of my query; this is the scenario:

my DB schema

based on this db schema, I know the naming part sucks a little, but this is
a project which has been already kicked off, so I've to stick with that.

Now, my goal is to select all the characters with a determined pack_id and a certain category_id, i.e. all the characters from pack 1 in category 5, so this's my NSPredicate

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(category_id == %@) AND (charRelationship.pack_id == %@)", [[cat valueForKey:@"category_id"] stringValue], curPack];
    [request setEntity:[NSEntityDescription entityForName:@"Category" inManagedObjectContext:self.managedObjectContext]];
    [request setPredicate:predicate];

    NSError *error;
    NSArray *result = [self.managedObjectContext executeFetchRequest:request error:&error];

as soon as the compiler tries to execute the fetchRequest, it crashes and goes SIGABRT.
I really hate the fact that xcode is not even giving me a clue about the exception so that I could figure it out myself. So after blindly trying to fix it with no success, I wonder if there's anybody out there who could help me.
I've already red a ton of other threads on SO and elsewhere, but I couldn't find any solution.

thanks a lot

-k-

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

方觉久 2025-01-15 04:09:14

尝试使用 ANY 来建立关系:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(category_id == %@) AND (ANY charRelationship.pack_id like %@)", [[cat valueForKey:@"category_id"] stringValue], curPack];

try using ANY for the relationship:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(category_id == %@) AND (ANY charRelationship.pack_id like %@)", [[cat valueForKey:@"category_id"] stringValue], curPack];
要走干脆点 2025-01-15 04:09:14

实际上,您不能为关联的实体单独存储任何 id。例如:在您的情况下,您可以只引用字符实体中的类别,例如:

[NSPredicate predicateWithFormat:@"catRel = %@", cat];

但是,我不确定您将如何处理该片段。我是否正确,您有一个类别实体存储在“cat”变量中,并且只需使用谓词从数据库中选择它?我想说,category_id 已经标识了您的类别...无论如何,尝试不要使用“charRelationship.pasck_id == %@”,而是加载相应的字符实体并在您刚刚放置实体的位置使用“charRelationship = %@”。

Actually you must not store any id separately for an entity that is associated. E.g.: in your case you could just refer to category from the characters entity like:

[NSPredicate predicateWithFormat:@"catRel = %@", cat];

However, I'm not sure what are you going to do with the snippet. Am I right that you have a Category entity stored in your 'cat' variable, and just going to select it from the database with the predicate? I would say, that a category_id already identifies your category... whatever, try not user "charRelationship.pasck_id == %@" but load the corresponding Character entity and use "charRelationship = %@" where you just put your entity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文