Best way to structure Core Data fetch for items, based on attribute(s) at other end of to-many relationship

aaronfalls

I am building a self-quiz style app. It is basically a collection view of full-screen slides. Each slide has multiple annotations that the user taps to test themselves. The slides are grouped into categories, the user can filter by category.

I would like to implement the ability to also filter the annotations based on whether the user has marked them known/not known/etc. My current data model (relevant parts) looks like this:

---------------        ---------------------------------------------
| Slide       |        | Annotation                                |
---------------        ---------------------------------------------
| name        |        | lastKnown (enum for known/not known/etc.) |
| category    |        | ...                                       |
---------------        ---------------------------------------------
| annotations | <--->> | slide                                     |        
---------------        ---------------------------------------------

Since the last known/not known/etc. response lives in the annotation object, it seems to me that I need to:

  1. Create a fetch (for my NSFetchedResultsController) on the slide entity
  2. Use a predicate for the selected categories (so I only get slides from that category)
  3. Filter on whether the slide has any annotations where the lastKnown matches the selected known/not known/etc filter (so that if a slide from that category does not have any annotations that match the selected known/not known/etc. filter, it is excluded)

I'm all set on 1 and 2, but having trouble on 3. My understanding at this point is that each slide's annotation relationship will return an NSSet of the annotation objects. It seems I need to test each of these to see if they match the criteria in the known/not known/etc. filter. It also seems to me that I should do it in a predicate, so I can just reload the collection view to refresh the data.

Can anyone offer some best practices on the most efficient way to approach 3?

Matt Morey

You should look into the ANY operation:

NSPredicate *slidePredicate = [NSPredicate predicateWithFormat:@"category == 'category'"];

NSPredicate *annotationPredicate = [NSPredicate predicateWithFormat:@"ANY annotations.lastKnown = 'known'"];

NSCompoundPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[slidePredicate, annotationPredicate]];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is best way to fetch data from two table with one to many relationship?

From Dev

Proper way to fetch two attributes in core data

From Dev

Best practice for preloading core data with many to many relationship

From Dev

How to insert and fetch to-many relationship entities in core data

From Dev

iOS Core Data to-many relationship insert/fetch

From Dev

core data one to many relationship, assign, fetch, predicate on NSSet

From Dev

What the best way to model a many to many relationship

From Dev

"Best" way for loading many items

From Dev

Core data and one to many relationship

From Dev

How to Only Add Objects with Distinct Attributes to Core Data to-many Relationship NSSet

From Dev

Many to many relationship to self in Core Data (iOS)

From Dev

Fetch all relationship data - Core Data

From Dev

Best way to build this data structure

From Dev

Best way to structure with many validating methods

From Dev

Core Data fetch entities with only unique attributes

From Dev

Core Data fetch entities with only unique attributes

From Dev

Best way to implement many-to-many relationship in MySQL

From Dev

Best way to query a Many to Many Relationship using pg-promise

From Dev

Best way to generate many to many relationship in Excel 2007?

From Dev

Core data Fetch request on relationship properties

From Dev

Core data fetch records Group by relationship?

From Dev

Best way to handle data attributes in Slim

From Dev

Core Data add object to-many relationship

From Dev

To-Many Relationship with iOS Core Data

From Dev

Core-Data NSFetchedResultsController to-many relationship

From Dev

iOS core data one to many relationship NSSET

From Dev

Core Data - sectionNameKeyPath with a One to Many Relationship

From Dev

Core data one to many relationship in swift

From Dev

Core-Data NSFetchedResultsController to-many relationship

Related Related

  1. 1

    What is best way to fetch data from two table with one to many relationship?

  2. 2

    Proper way to fetch two attributes in core data

  3. 3

    Best practice for preloading core data with many to many relationship

  4. 4

    How to insert and fetch to-many relationship entities in core data

  5. 5

    iOS Core Data to-many relationship insert/fetch

  6. 6

    core data one to many relationship, assign, fetch, predicate on NSSet

  7. 7

    What the best way to model a many to many relationship

  8. 8

    "Best" way for loading many items

  9. 9

    Core data and one to many relationship

  10. 10

    How to Only Add Objects with Distinct Attributes to Core Data to-many Relationship NSSet

  11. 11

    Many to many relationship to self in Core Data (iOS)

  12. 12

    Fetch all relationship data - Core Data

  13. 13

    Best way to build this data structure

  14. 14

    Best way to structure with many validating methods

  15. 15

    Core Data fetch entities with only unique attributes

  16. 16

    Core Data fetch entities with only unique attributes

  17. 17

    Best way to implement many-to-many relationship in MySQL

  18. 18

    Best way to query a Many to Many Relationship using pg-promise

  19. 19

    Best way to generate many to many relationship in Excel 2007?

  20. 20

    Core data Fetch request on relationship properties

  21. 21

    Core data fetch records Group by relationship?

  22. 22

    Best way to handle data attributes in Slim

  23. 23

    Core Data add object to-many relationship

  24. 24

    To-Many Relationship with iOS Core Data

  25. 25

    Core-Data NSFetchedResultsController to-many relationship

  26. 26

    iOS core data one to many relationship NSSET

  27. 27

    Core Data - sectionNameKeyPath with a One to Many Relationship

  28. 28

    Core data one to many relationship in swift

  29. 29

    Core-Data NSFetchedResultsController to-many relationship

HotTag

Archive