mirx
mirx

Reputation: 158

CoreData: when is it okay to use several fetchResultsControllers?

Still in my RSS reader development, I use the following model:

Categorie -> Feed -> Post

For my Master View Controller, I want to display the number of unread Posts (Posts which read NSDate is nil) next to the category name.

As I need an NSFetchResultsController to select the Categories, do I need another one to get the Category.feeds.posts.read == nil count?

How would you do it?

Upvotes: 1

Views: 98

Answers (1)

Mundi
Mundi

Reputation: 80271

No, you don't need a separate fetched results controller.

The way I understand your question: you want to display a list of categories. With each category name you want to display the count of unread messages.

I would implement a fetched property in your Category class like this.

@implementation Category ()

-(NSUInteger)unreadMessages {
   NSUInteger count = 0;
   for (Feed *feed in self.feeds) {
      NSSet *posts = [feed.posts filteredSetUsingPredicate
       [NSPredicate predicateWithFormat:@"read = null"]];
      count += posts.count
   }
   return count;
}

@end

It would be even more efficient, I think, if you would introduce a flag property unread (rename read to firstReadDate) with default set to 1 for post:

for (Feed *feed in self.feeds) {
  count += [[feed.posts valueForKeyPath:@"@sum.unread"] integerValue];
}

Upvotes: 2

Related Questions