使用嵌套的NSPredicate SUBQUERY返回空数组

时间:2015-09-04 00:49:40

标签: ios core-data nspredicate

这是我的对象图的相关部分:

Member_Profile <-->> Member_Posts <-->> Comment

object_graph

我希望能够通过member_id属性过滤帖子,如下所示:

@"(member_id == %@)"

当用户选择帖子时,我希望能够通过member_id属性过滤所选帖子的评论,如下所示:

memberPosts.comment.post_id == 7

我正在尝试使用一组嵌套的SUBQUERY谓词,如下所示:

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"(member_id == %@) AND (0 != SUBQUERY(memberPosts, $x, (0 != SUBQUERY($x.comment, $y, ANY $y.post_id == %@).@count)).@count)", _memberIDString, _postIDString];

这会在运行时返回空数组

这似乎我在正确的轨道上,但是找不到任何其他使用相同语法的示例。我错过了什么?

编辑1:

这是我的完整NSFetchedResultsController

- (NSFetchedResultsController *)fetchPostCommentsFromContext {

if (!_fetchPostCommentsFromContext) {

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:MEMBER_PROFILE_ENTITY];

    fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"member_id" ascending:YES]];        

    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"(member_id = %@) AND (0 != SUBQUERY(memberPosts, $x, (0 != SUBQUERY($x.comment, $y, ANY $y.post_id = %@).@count)).@count)", _memberIDString, _memberPostObj.post_id];

    NSManagedObjectContext *managedObjectContextObj= [RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext;

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

    self.fetchPostCommentsFromContext.delegate = self;

    NSError *error;

    [self.fetchPostCommentsFromContext performFetch:&error];
    NSAssert(!error, @"Error performing fetch request: %@", error);

    NSLog(@"%@",[self.fetchPostCommentsFromContext fetchedObjects]);
}
return _fetchPostCommentsFromContext;
}

编辑2:

以下是我要提取memberProfile的API请求:

- (void)requestMemberProfileAPI {

NSString *requestPath = [AndTunesConstants requestPath:MEMBER_PROFILE_API];

NSDictionary *parameters = @{ @"member_id": _memberIdToFetchProfileData,
                              @"timestamp": @"0",
                              };

[[RKObjectManager sharedManager]
 postObject:nil path:requestPath parameters:parameters success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResutl) {

     NSError *error;
     NSDictionary *json = [NSJSONSerialization JSONObjectWithData:operation.HTTPRequestOperation.responseData
                                                          options:kNilOptions
                                                            error:&error];
     if (error == nil)
     {
         if ([[json objectForKey:@"status"] isEqualToString:@"true"])
         {

                 if (![self.fetchPostCommentsFromContext performFetch:&error])
                 {
//                     abort();
                 }

         } else {
             NSLog(@"Server Response False (status = false)");
         }
     } else {
         NSLog(@"Error in Serializing JSON (json not correct): %@", error.localizedDescription);
     }
 }
 failure:^(RKObjectRequestOperation *operation, NSError *error) {
     RKLogError(@"Load failed with error: %@", error);

 }];
}

0 个答案:

没有答案