具有多个条件/关系的coredata子查询

时间:2015-01-14 15:48:23

标签: core-data subquery nspredicate

我有以下型号:

enter image description here

如何获得groupId =='2'的所有PT对象?

我尝试了几种方法,但没有成功。

使用此查询,我只得到一个结果:

            NSFetchRequest *request = [[NSFetchRequest alloc] init];
            request.entity = [NSEntityDescription entityForName:@"Status" inManagedObjectContext:self.managedObjectContext];
            request.predicate = [NSPredicate predicateWithFormat:@"group.groupId == %@", [NSNumber numberWithInt:2]];
            NSError *error = nil;
            NSArray *resultSet = [self.managedObjectContext executeFetchRequest:request error:&error];
            [request release];

            NSLog(@"array count: %lu", (unsigned long)[resultSet count]);

            NSPredicate *thePredicate = [NSPredicate predicateWithFormat:@"hasBeenDeleted == %@ AND (ANY hasStatus IN %@)",
                                         [NSNumber numberWithBool:NO],
                                         [NSSet setWithArray:resultSet]];

            [self.ptListViewController refreshDataWithPredicate:thePredicate];

2 个答案:

答案 0 :(得分:2)

您可以从模板开始" GROUP_BY_GROUP_ID"并将其添加到managedObjectModel ...

    - (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:@"GroupdId2" withExtension:@"momd"];
        _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];


        NSFetchRequest *tmpTemplate;
        NSEntityDescription *tmpEntity;
        NSPredicate *tmpPredicate;

        // 1. GROUP_BY_GROUP_ID
        tmpEntity = [[_managedObjectModel entitiesByName] objectForKey:@"Group"];
        tmpTemplate = [[NSFetchRequest alloc] init];
        [tmpTemplate setEntity:tmpEntity];
        tmpPredicate = [NSPredicate predicateWithFormat:@"(groupId == $value)"];
        [tmpTemplate setPredicate:tmpPredicate];
        [_managedObjectModel setFetchRequestTemplate:tmpTemplate forName:@"GROUP_BY_GROUP_ID"];

        return _managedObjectModel;
    }

这是适当的获取方法:

    - (NSArray *)fetchGroupByGroupId:(NSNumber *)value
    {
        NSError *error = nil;

        NSManagedObjectModel *model = _managedObjectModel;

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

        NSFetchRequest *fetchRequest = [model fetchRequestFromTemplateWithName:@"GROUP_BY_GROUP_ID" substitutionVariables:substitutionDictionary];

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

        return results;
    }

现在您可以开始像这样获取PT实体(Xcode允许您为所有实体生成NSManagedObject子类):

    NSArray *groups=[self fetchGroupByGroupId:@(2)];
    NSMutableSet *resultPT=[[NSMutableSet alloc] init];
    for (Group *group in groups) {
        for (Status *status in group.hasStatus) {
            for (PTStatus *ptstatus in status.ptStatus) {
                if (ptstatus.targetPT!=nil) {
                    [resultPT addObject:ptstatus.targetPT];
                }
                if (ptstatus.pt!=nil) {
                    [resultPT addObject:ptstatus.pt];
                }
            }
        }
    }

resultPT现在,您将找到所有不同的PT实体"使用" groupId ==' 2'。

您可以在此处找到XCode 6.1项目:XCode 6.1 project on Dropbox

希望它有所帮助。

答案 1 :(得分:0)

我找到了! 将状态与集团联系起来时,我遇到了一个错误。 正确的代码:

            NSFetchRequest *request = [[NSFetchRequest alloc] init];
            request.entity = [NSEntityDescription entityForName:@"PTStatus" inManagedObjectContext:self.managedObjectContext];
            request.predicate = [NSPredicate predicateWithFormat:@"ANY status.group.groupId == %@", [NSNumber numberWithInt:2]];
            NSError *error = nil;
            NSArray *resultSet = [self.managedObjectContext executeFetchRequest:request error:&error];
            [request release];

            NSLog(@"array count: %lu", (unsigned long)[resultSet count]);

            NSPredicate *thePredicate = [NSPredicate predicateWithFormat:@"hasBeenDeleted == %@ AND (ANY hasStatus IN %@)",
                                         [NSNumber numberWithBool:NO],
                                         [NSSet setWithArray:resultSet]];

            [self.ptListViewController refreshDataWithPredicate:thePredicate];

[self.ptListViewController refreshDataWithPredicate:thePredicate];

- (void)refreshDataWithPredicate:(NSPredicate *)predicate{
    self.fetchedResultsController = nil;
    [NSFetchedResultsController deleteCacheWithName:@"PTs_Cache"];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:@"PT" inManagedObjectContext:self.managedObjectContext];

    request.sortDescriptors = [NSArray arrayWithObjects:
                               [NSSortDescriptor sortDescriptorWithKey:@"requestDate" ascending:NO],
                               [NSSortDescriptor sortDescriptorWithKey:@"number" ascending:YES],
                               nil];

    request.predicate = predicate;

    request.fetchBatchSize = 40;

    NSString *sectionNameKeyPathString = @"dateSection_transient";
    NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                          managedObjectContext:self.managedObjectContext
                                                                            sectionNameKeyPath:sectionNameKeyPathString
                                                                                     cacheName:@"PTs_Cache"];
    [request release];

    self.fetchedResultsController = frc;
    [frc release];

    [self.theTable reloadData];
}