在一对多关系中使用 NSPredicate 进行过滤

发布于 2024-12-29 10:57:51 字数 2068 浏览 1 评论 0原文

我在 stackoverflow 上尝试了很多解决方案,但找不到有效的解决方案。我有一个包含两个实体的核心数据模型:客户端和目的地。两者都由 NSManagedObject 子类包装。

客户端具有一些属性和称为目的地的一对多关系。 Destination 有一个名为 default_dest 的属性,该属性由 NSNumber 和称为 client 的逆关系包装。

我有一个 UITableViewController ,其中使用以下 fetchedController 属性。该请求效果良好。我能够检索存储在 SQLite 中的客户端。

if (fetchedResultsController) 
    return fetchedResultsController;

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Client" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

[fetchRequest setFetchBatchSize:20];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"code" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];

fetchedResultsController.delegate = self;

[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];

return fetchedResultsController;

我会再迈出一步。我现在将为每个客户端过滤从先前请求检索的目标(包含在目标 NSSet 中)。特别是,只有当其 default_dest 值为 1 时才能添加目标。

为了修复此规范,我尝试添加一个 NSPredicate ,如下所示:

NSPredicate* predicate = [NSpredicate predicateWithFormat:@"ANY destinations.default_dest == %@", [NSNumber numberWithInt:1]];

然后我在 fetchRequest 中将其设置为:

[fetchRequest setPredicate:predicate];

每次运行请求时,它返回“多关系故障目的地...”。这是什么意思?

我已阅读 iphone-core-data-relationship-fault 但我没有明白这是什么意思。

所以,我的问题是:是否有可能达到类似的目标?如果是,您有什么建议吗?

注释

显然,我可以迭代设置的目的地,但我不知道是否会是一个昂贵的迭代以及有多少记录。

I tried a lot of the solutions in stackoverflow but I'm not able to find a valid one. I have a core data model with two entities: Client and Destination. Both are wrapped by NSManagedObjectsubclasses.

Client has some properties and a one-to-many relationship called destinations.
Destination has a property called default_dest that is wrapped by a NSNumber and an inverse relationship called client.

I have a UITableViewController where I'm using the following fetchedController property. The request works well. I'm able to retrieve clients stored in SQLite.

if (fetchedResultsController) 
    return fetchedResultsController;

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Client" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

[fetchRequest setFetchBatchSize:20];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"code" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];

fetchedResultsController.delegate = self;

[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];

return fetchedResultsController;

I would make another step. I would now filter the destinations retrieved from the previous request (that are contained in destinations NSSet) for each client. In particular, the destination can be added only if its default_dest value is 1.

To fix this specification I tried to add an NSPredicate like the following:

NSPredicate* predicate = [NSpredicate predicateWithFormat:@"ANY destinations.default_dest == %@", [NSNumber numberWithInt:1]];

Then I set it in fetchRequest as:

[fetchRequest setPredicate:predicate];

Each time I run the request, it returns a "to-many-relationship fault destinations...". What does it mean?

I've read iphone-core-data-relationship-fault but I don't understand what does it mean.

So, my questions are: Is it possible to reach a similar goal? If yes, do you have any suggestions?

Notes

Obviously I could iterate over destinations set but I don't know if could be an expensive iteration and how many records there are.

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

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

发布评论

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

评论(2

岁月无声 2025-01-05 10:57:51

对于那些感兴趣的人。

您需要对您的获取请求说要使用以下方式预取关系

- (void)setRelationshipKeyPathsForPrefetching:(NSArray *)keys

: 例如:

[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects: @"destinations", nil]];

以这种方式,Core Data 预取您指定的关系并且不会引发错误。

此外,您可以通过使用限制来限制获取请求的结果数量:

- (void)setFetchLimit:(NSUInteger)limit

希望有帮助。

For those interested in.

You need to say to your fetch request to prefetch relationships using

- (void)setRelationshipKeyPathsForPrefetching:(NSArray *)keys

For example:

[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects: @"destinations", nil]];

In this manner Core Data prefetch the relationships you have specified and doesn't fire fault.

In addition you can limit the number of results for your fetch request by limiting it using:

- (void)setFetchLimit:(NSUInteger)limit

Hope it helps.

我早已燃尽 2025-01-05 10:57:51

这是在 fetch 中检索特定值的不同方法。也许这可以帮助(在文档中查找链接名称):

获取特定值

Here is a different way to retrieve specific values in fetch. Maybe this can help (look up link name in document):

Fetching Specific Values

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