0

I have one CoreData model object that includes a type named sessionID (a uuid generated every time the user uses a feature in the app) - data keep coming in and recorded with the same sessionID for each session.

The recorded data would look like this: x number of lines with lets say sessionID:...1 and then y number of records for sessionID:...2

```

sessionID: 0000-0000-0000-0000-0001, ... 
sessionID: 0000-0000-0000-0000-0001, ... 
sessionID: 0000-0000-0000-0000-0002, ... 
sessionID: 0000-0000-0000-0000-0002, ...
sessionID: 0000-0000-0000-0000-0002, ...

Etc. ```

I am using NSFetchResultsController to present the records to the user on a UITableView, (and it works) However, I would like to have an option toggle between presentation of all records and of only the unique sessionID as rows on the table view. Is that possible with NSFetchResultController?

While I am setting the request to return distinct results, it does not seem to work, I am still getting all the records.

```

[fetchRequest setEntity:entity];
[fetchRequest setReturnsDistinctResults:YES];
[fetchRequest setPropertiesToFetch:@[@"session_id"]];

```

Koen
  • 49
  • 1
  • 6
  • You can change the fetch request anytime you need, just make sure cache is deleted. – sschale Jul 25 '17 at 06:07
  • Possible duplicate of [Why we can't change FetchRequest at NSFetchedResultsController?](https://stackoverflow.com/questions/7200855/why-we-cant-change-fetchrequest-at-nsfetchedresultscontroller) – sschale Jul 25 '17 at 06:07

1 Answers1

0

returnsDistinctResults does not work for a fetchedResultsController. Use sectionNameKeyPath of the fetchedResultsController to group by sessionId.

The result will be many sections, with each section containing entries with the same sessionIds.

When you when to show all of the entries then show no header for the section and display every row in every section. When you want to show only the SessionId then show one row per section. You can get the sessionId by looking at the first element in the section.

This does not require you to change the fetchedResultsController at all - you are only changing how you are showing the information. Depending on your UI/UX you may find it easier to have two tableviews and the toggle shows and hides them. Or it might be easier to set a BOOL in your controller and call [tableview reloadData] when you toggle modes.

Jon Rose
  • 8,373
  • 1
  • 30
  • 36