NSPredicate, get results with a subset of one-to-many relationship

后端 未结 3 1515
旧时难觅i
旧时难觅i 2021-01-07 02:45

I\'mm working around with Core Data and NSFetchedResultsController.

My Data Model looks like this:

Product with one-to-many relat

3条回答
  •  耶瑟儿~
    2021-01-07 03:05

    You should fetch the dataLine property instead. Assuming your Product and dataLine entity connected by relationship someRelation then you can try this code;

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:[NSEntityDescription entityWithName:@"dataLine" inManagedObjectContext:self.managedObjectContext]];
    [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"dataLines.week == %@",theWeek]];
    
    NSMutableArray *tmpProduct [[NSMutableArray init] alloc];
    NSMutableArray *tmpArray = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    
    for (dataLine *theDataLine in tmpArray);
    NSLog(@"%@",theDataLine.someRelation.name);
    tmpProduct = theDataLine.someRelation.name;
    

    then you can just call tmpProduct to call or display your product in table view

提交回复
热议问题