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

OneManBand

Suppose that I have a core data entity ShapesEntry which contains several relationships:

  • shapeInstances (to-many w/ ShapeInstance Entity)
  • sideCounts (to many w/ String_Container Entity)
  • edgeLengths (to many w/ String_Container Entity)
  • colors (to many w/ String_Container Entity)

The ShapeInstance entity has three attributes: numSides, edgesLength, color. Inverse to ShapeEntry.

The String_Container entity has one attribute: stringValue. Inverse to ShapeEntry.

The purpose of ShapesEntry is to contain all instances of existing shapes, as well as to keep track of the distinct numSides, edgesLengths, and colors that every recorded shape instance uses.

For example:

  • (Triangle): 3-5-RED
  • (Square): 4-5-GREEN
  • (Square): 4-10-BLUE
  • (Pentagon): 3-10-GREEN

^ After adding these shape instances, GameEntry should include each of these four Shape objects, and the NSSet relationships to sideCounts, edgeLengths, and colors should include {3, 4}, {5, 10}, and {RED, GREEN, BLUE}, respectively.

HOWEVER, because String_Container is an object...

String_Container *sides = [NSEntity Description insertNewObjectForEntityForName:@"String_Container" inManagedObjectContext:context];
sides.stringValue = shapeInstance.numSides; //or edgesLength or color
[shapeEntry addSideCounts:sides];

...the above code renders sideCounts, edgeLengths, and colors of {3, 4, 4, 3}, {5, 5, 10, 10}, and {RED, GREEN, BLUE, GREEN} and the addObject method does not recognize them as distinct.

I COULD fetch the appropriate ShapeEntry (this example is dumbed down, so only 1 of these exists here, but in my code you could have thousands), iterate through the existing attributes for each of the relationships and confirm that the value I want to add does not already exist BUT that seems awfully expensive when what I really want is for the relationship to act like an NSSet from the get-go. I do recognize that technically the relationship is doing its job because the objects I want to add are 'distinct objects' despite having identical attribute values, but I would like it to go a step deeper and confirm that these attribute values are distinct before trying to add to the ShapeEntry relationship.

How can I accomplish this task? If my example was confusing, please ask for clarification...I made it up on the spot so hopefully my variable names are consistent...

Thanks in advance!!

Martin R

Using the Key-Value Coding method

NSSet *colors = [shapeEntry valueForKeyPath:@"shapeInstances.color"];

you get all color values of the shape instances related to shapeEntry, the same works for the other attributes. This seems to be easier than to duplicate all the information.

You still have the option to "cache" the colors set in a transient property of ShapeEntry if necessary.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

iOS core data one to many relationship NSSET

From Dev

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

From Dev

Core Data relationship, NSSet & ManagedObjectContext

From Dev

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

From Dev

Core Data add object to-many relationship

From Dev

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

From Dev

IOS/Core-Data: add many to many relationship

From Dev

How can I add data to core data using one-to-many relationship?

From Dev

Getting objects from to-many relationship in Swift Core Data

From Dev

sorting data of type NSSet of a to-many relationship using NSSortDescriptor

From Dev

How to trigger notifications when item added to or removed from Core Data NSManagedObject relationship's NSSet?

From Dev

How to trigger notifications when item added to or removed from Core Data NSManagedObject relationship's NSSet?

From Dev

Core-data: querying distinct entities in many to many relationship with middle entity and condition

From Dev

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

From Dev

Core Data add identical object to to-many relationship

From Dev

How to apply NSSortdiscriptor or NSPredicate on a NSSet of a to-many relationship

From Dev

Inverse Relationship with Core Data Causes Crash when Adding Object to NSSet

From Dev

How to Add target NSManagedObject to another one with an inverse many-to-many Core Data relationship without duplicating target NSManagedObject?

From Dev

Swift: How to check a core data NSSet for an object

From Dev

Swift: How to check a core data NSSet for an object

From Dev

Core data and one to many relationship

From Dev

How to add and show some attributes to a many2many relationship (OpenERP7)?

From Dev

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

From Dev

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

From Dev

How to handle a one-to-many relationship in CORE DATA

From Dev

Core data: NSFetchedResultsController with objects are in a relationship

From Dev

Core data: NSFetchedResultsController with objects are in a relationship

From Java

One to many relationship with objects using EF core

From Dev

Core Data - NSSet of NSString

Related Related

  1. 1

    iOS core data one to many relationship NSSET

  2. 2

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

  3. 3

    Core Data relationship, NSSet & ManagedObjectContext

  4. 4

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

  5. 5

    Core Data add object to-many relationship

  6. 6

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

  7. 7

    IOS/Core-Data: add many to many relationship

  8. 8

    How can I add data to core data using one-to-many relationship?

  9. 9

    Getting objects from to-many relationship in Swift Core Data

  10. 10

    sorting data of type NSSet of a to-many relationship using NSSortDescriptor

  11. 11

    How to trigger notifications when item added to or removed from Core Data NSManagedObject relationship's NSSet?

  12. 12

    How to trigger notifications when item added to or removed from Core Data NSManagedObject relationship's NSSet?

  13. 13

    Core-data: querying distinct entities in many to many relationship with middle entity and condition

  14. 14

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

  15. 15

    Core Data add identical object to to-many relationship

  16. 16

    How to apply NSSortdiscriptor or NSPredicate on a NSSet of a to-many relationship

  17. 17

    Inverse Relationship with Core Data Causes Crash when Adding Object to NSSet

  18. 18

    How to Add target NSManagedObject to another one with an inverse many-to-many Core Data relationship without duplicating target NSManagedObject?

  19. 19

    Swift: How to check a core data NSSet for an object

  20. 20

    Swift: How to check a core data NSSet for an object

  21. 21

    Core data and one to many relationship

  22. 22

    How to add and show some attributes to a many2many relationship (OpenERP7)?

  23. 23

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

  24. 24

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

  25. 25

    How to handle a one-to-many relationship in CORE DATA

  26. 26

    Core data: NSFetchedResultsController with objects are in a relationship

  27. 27

    Core data: NSFetchedResultsController with objects are in a relationship

  28. 28

    One to many relationship with objects using EF core

  29. 29

    Core Data - NSSet of NSString

HotTag

Archive