Core Data: Fetching related objects in many-to-many relationship

John Wickham

I'm trying to wrap my head around the proper architecture/pattern for this situation:

I've got two entities, Book and Librarys. They each have a many-to-many relationship to the other (Books can be in many Librarys, and a Library will have many Books.

One of my views lists books in a particular library. I have a controller class that handles fetching of this data and provides it to my view. To do that, I'm planning on using an NSFetchRequest for all Book entities, filtered by a predicate that fetches only books in a specific Library. But I can't seem to find the proper way to format the predicate for this fetch request.

I also investigated simply accessing Library's books accessor to get access to the appropriate books without having to fetch anything (as described here), but I want to use NSFetchedResultsControllerDelegate so my controller is notified about any changes to the fetched objects, and can notify the view. I considered just listening for NSManagedObjectContextDidChangeObjectsNotification, but this will deliver notifications for every single change in the context, even if it's not for relevant entities.

How have you handled situations like this in the past?

André Slotta

Your fetch request / predicate could look something like this:

let fetchRequest: NSFetchRequest<Book> = Book.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "%K CONTAINS %@", #keyPath(Book.libraries), library)

// Another option:
fetchRequest.predicate = NSPredicate(format: "SELF IN %@", library.books!)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

saving, deleting and fetching data from a one to many relationship core data

From Dev

Core Data - Order of objects in a to-many relationship

From Dev

Getting objects from to-many relationship in Swift Core Data

From Dev

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

From Dev

JPA with Hibernate - Many to Many relationship, fetching all data

From Dev

Core data and one to many relationship

From Dev

Django getting related objects in a many-to-many relationship

From Java

One to many relationship with objects using EF core

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

From Dev

Core Data to-many relationship filterContentForSearchText with NSPredicate

From Dev

Core Data one-to-many inverse relationship

From Dev

Correct Delete Rule for Core Data Many-to-Many Relationship?

From Dev

Core Data Join Table Many to Many Relationship - Object Creation

From Dev

Core Data Many-to-Many relationship update causes fault

From Dev

Allowing duplicate records in Core Data Many-to-many relationship

From Dev

Best practice for preloading core data with many to many relationship

From Dev

How to make a many to many relationship in core data in the .xcdatamodel

From Dev

IOS/Core-Data: add many to many relationship

From Dev

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

From Dev

Core data relationship lost after fetching more objects into the entities

From Dev

Related name for recursive many to many relationship not working

From Dev

ActiveRecord fetching records from has_many and related relationship through scopes

From Dev

Fetching data with relationship in core data

Related Related

  1. 1

    saving, deleting and fetching data from a one to many relationship core data

  2. 2

    Core Data - Order of objects in a to-many relationship

  3. 3

    Getting objects from to-many relationship in Swift Core Data

  4. 4

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

  5. 5

    JPA with Hibernate - Many to Many relationship, fetching all data

  6. 6

    Core data and one to many relationship

  7. 7

    Django getting related objects in a many-to-many relationship

  8. 8

    One to many relationship with objects using EF core

  9. 9

    Core Data add object to-many relationship

  10. 10

    To-Many Relationship with iOS Core Data

  11. 11

    Core-Data NSFetchedResultsController to-many relationship

  12. 12

    iOS core data one to many relationship NSSET

  13. 13

    Core Data - sectionNameKeyPath with a One to Many Relationship

  14. 14

    Core data one to many relationship in swift

  15. 15

    Core-Data NSFetchedResultsController to-many relationship

  16. 16

    Core Data to-many relationship filterContentForSearchText with NSPredicate

  17. 17

    Core Data one-to-many inverse relationship

  18. 18

    Correct Delete Rule for Core Data Many-to-Many Relationship?

  19. 19

    Core Data Join Table Many to Many Relationship - Object Creation

  20. 20

    Core Data Many-to-Many relationship update causes fault

  21. 21

    Allowing duplicate records in Core Data Many-to-many relationship

  22. 22

    Best practice for preloading core data with many to many relationship

  23. 23

    How to make a many to many relationship in core data in the .xcdatamodel

  24. 24

    IOS/Core-Data: add many to many relationship

  25. 25

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

  26. 26

    Core data relationship lost after fetching more objects into the entities

  27. 27

    Related name for recursive many to many relationship not working

  28. 28

    ActiveRecord fetching records from has_many and related relationship through scopes

  29. 29

    Fetching data with relationship in core data

HotTag

Archive