1
votes

I have 2 entities : Post & Category , Post entity has relationship to one with Category, each Post has One Category.

Post           Category
----           --------
postId         categoryId
title          title
slug           slug
category ----> posts

what i need here is NSFetchRequest to fetch posts by date, then to groupBy Category.

so result would be something like:

Category1:
  - Post 1
  - Post 2
Category 2:
 - Post 3
 - Post 4

i've tried :

[_fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObjects:@"category.categoryId", nil]];

and :

[_fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObjects:@"category", nil]];

with no success. Any suggestions ?

Thanks.

1

1 Answers

0
votes

Can you try something like this:

//context is the NSManagedObjectContext
NSEntityDescription* post = [NSEntityDescription entityForName:@"Post"
                                              inManagedObjectContext:context];
NSAttributeDescription* catDesc = [post.attributesByName objectForKey:@"category"];
[_fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObjects:catDesc, nil]];