iOS NSPredicate由父实体过滤核心数据

时间:2014-09-27 23:58:49

标签: ios objective-c core-data nspredicate nsfetchedresultscontroller

所以我迷失在这一个。我试图使用谓词获取父对象的子对象。如果我取出谓词行,它将返回所有特定帖子。所以我很确定这是谓词的问题。任何帮助将不胜感激!以下是我的设置。

AccountTimeLine - > SpecificPosts(一对多)

核心数据模型: enter image description here

enter image description here

获取结果控制器:

- (void)specificPostFetch
{
    DataAccessLayer *delegate = [[DataAccessLayer alloc]init];
    self.managedObjectContext = delegate.managedObjectContext;
    NSString *entityName = @"SpecificPosts";
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"createdDateSort" ascending:YES]];
    request.predicate = [NSPredicate predicateWithFormat:@"accountTimeLine = %@", self.accountTimeLineObject];
    [request setFetchBatchSize:20];
    self.specificPostFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    self.specificPostFetchedResultsController.delegate = self;
    [self.specificPostFetchedResultsController performFetch:nil];
}

打印出用于在谓词

中进行比较的accountTimeLineObject
po self.accountTimeLineObject
<AccountTimeLine: 0x7c087b70> (entity: AccountTimeLine; id: 0x7c086590 <x-coredata://353F201D-E739-4DD0-BEBF-DAD6101CFE51/AccountTimeLine/p72> ; data: {
    accountType = 0;
    nextPageUrl = 515986477864337411;
    sort = 0;
    specificPosts = "<relationship fault: 0x7c634590 'specificPosts'>";
    userAccountId = 582;
})

1 个答案:

答案 0 :(得分:0)

尝试将==放在谓词中,而不是单个=:

request.predicate = [NSPredicate predicateWithFormat:@"accountTimeLine == %@", self.accountTimeLineObject];