gpt4 book ai didi

ios - 如何在 NSPredicate 中使用 “ANY” 聚合操作来过滤 CoreData 多对多关系

转载 作者:行者123 更新时间:2023-11-29 12:35:18 25 4
gpt4 key购买 nike

数据模型:

enter image description here

enter image description here

enter image description here

我正在尝试获取所有具有 contentType.selected == 1 的“解决方案”

证明结构和数据设置正确

--ContentType 实体数据 Content Type Entity Data

--解决方案实体数据

enter image description here

--ContentType关系

enter image description here

完整性检查(无谓词)

(lldb) po self.solutionTreeFetchedResultsController.fetchedObjects
<_PFArray 0x7fe958ea2b10>(
<Solutions: 0x7fe95b1415e0> (entity: Solutions; id: 0xd00000000ac00006 <x-coredata://2799F23D-B41B-48EF-923A-9E2C2A0C10B5/Solutions/p688> ; data: <fault>),
<Solutions: 0x7fe95b141b10> (entity: Solutions; id: 0xd00000000ac40006 <x-coredata://2799F23D-B41B-48EF-923A-9E2C2A0C10B5/Solutions/p689> ; data: <fault>)

......去掉其余部分,但这里有 49 个。)

(lldb) po [self.solutionTreeFetchedResultsController.fetchedObjects count]
49

方法 1(最直接)

NSArray *contentTypes = self.contentTypeFetchedResultsController.fetchedObjects;
request.predicate = [NSPredicate predicateWithFormat:@"ANY contentType IN %@", contentTypes];

po contentTypes
<_PFArray 0x7fa4b6018680>(
<ContentType: 0x7fa4b60181c0> (entity: ContentType; id: 0xd0000000006c0004 <x-coredata://2799F23D-B41B-48EF-923A-9E2C2A0C10B5/ContentType/p27> ; data: <fault>),
<ContentType: 0x7fa4b6018620> (entity: ContentType; id: 0xd000000000700004 <x-coredata://2799F23D-B41B-48EF-923A-9E2C2A0C10B5/ContentType/p28> ; data: <fault>)
)

po self.solutionTreeFetchedResultsController.fetchedObjects
<__NSArrayI 0x7fa4b3c1ddf0>(

)

方法二

 request.predicate = [NSPredicate
predicateWithFormat:@"SUBQUERY(contentType, $contenttype, $contenttype IN %@).@count = %d", contentTypes, [contentTypes count]];

(lldb) po self.solutionTreeFetchedResultsController.fetchedObjects
<__NSArrayI 0x7fcea3e01350>(

)

方法三

NSMutableArray *predicates = [[NSMutableArray alloc] init];
for (ContentType *contentType in contentTypes) {
[predicates addObject:[NSPredicate predicateWithFormat:@"ANY contentType.name MATCHES %@", contentType.name]];
}

[predicates addObject:[NSPredicate predicateWithFormat:@"active == 1"]];
NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];
request.predicate = compoundPredicate;

(lldb) po self.solutionTreeFetchedResultsController.fetchedObjects
<__NSArrayI 0x7f8f9850bbf0>(

)

如果您只选择 1 个 contentType,则方法 3 可以工作,但如果您选择了 1 个以上,则它不会返回任何内容。

我迷路了,不知道还能尝试什么。任何投入将不胜感激!如果您还需要什么,请告诉我。谢谢!

已经尝试了这篇文章中的大部分解决方案,但似乎没有 100% 有效 How to use the "ALL" aggregate operation in a NSPredicate to filter a CoreData-based collection

最佳答案

如果您的问题是如何使用给定的数据模型获取所有具有 contentType.selected == 1 的“解决方案”,谓词:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY contentType.selected == $SELECTVALUE)"];

对我有用。将其定义为模板并像这样将其添加到模型中....

- (NSManagedObjectModel *)managedObjectModel {
// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"ANYApp" withExtension:@"momd"];

_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

NSEntityDescription *entity = [[_managedObjectModel entitiesByName] objectForKey:@"Solutions"];

NSFetchRequest *template = [[NSFetchRequest alloc] init];

[template setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY contentType.selected == $SELECTVALUE)"];

[template setPredicate:predicate];

[_managedObjectModel setFetchRequestTemplate:template forName:@"SolutionsTemplate"];

return _managedObjectModel;
}

像这样定义获取方法....

- (NSArray *)fetchAllSolutionsBySelected:(NSNumber *)selected
{
NSError *error = nil;

NSString *templateName=@"SolutionsTemplate";

NSDictionary *substitutionDictionary = [NSDictionary dictionaryWithObjectsAndKeys:selected, @"SELECTVALUE", nil];

NSFetchRequest *fetchRequest = [[_app managedObjectModel] fetchRequestFromTemplateWithName:templateName substitutionVariables:substitutionDictionary];

NSArray *results = [[_app managedObjectContext] executeFetchRequest:fetchRequest error:&error];

return results;
}

我终于可以获取所有具有 contentType.selected == 1 的“解决方案”:

NSArray *solutionsWithContentSelected=[self fetchAllSolutionsBySelected:@(1)];

您可以在此处找到 XCode 6.1 iOS 8.1 项目:ANYApp Project .希望对您有所帮助。

关于ios - 如何在 NSPredicate 中使用 “ANY” 聚合操作来过滤 CoreData 多对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26531349/

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