Count the number of managed objects in a @SectionedFetchRequest

I am using a @SectionedFetchRequest with some success.

This sectioned fetch request feeds data to a List().

I want to include a footer to the list that provides a count of the managed objects returned from the sectioned fetch request.

Previously I have used .count.

This still works but only returns the number of sections.

A sectioned fetch request provides this result...

SectionedFetchResults<String, CoreDataEntityClass>

So how do I iterate through this unusual collection to determine a count?

Accepted Reply

According to the doc, SectionedFetchResults is a RandomAccessCollection of Section and Section is a RandomAccessCollection of Result (Result == CoreDataEntityClass, in your case).

So, Please try something like this to get the total count:

items.reduce(0, {$0 + $1.count})
  • Thank you for explaining the SectionedFetchResults as well as providing an answer that works perfectly!

Add a Comment

Replies

According to the doc, SectionedFetchResults is a RandomAccessCollection of Section and Section is a RandomAccessCollection of Result (Result == CoreDataEntityClass, in your case).

So, Please try something like this to get the total count:

items.reduce(0, {$0 + $1.count})
  • Thank you for explaining the SectionedFetchResults as well as providing an answer that works perfectly!

Add a Comment