How do I remove/replace objects in an ordered to-many CoreData relationship?

Brock Boland

I've got a Contact entity with a one-to-many relationship to another entity that stores information about email addresses (it's called TypedValue because it also includes the email type, like personal, work, etc).

This relationship (called "emails," naturally), is also ordered, so that I can display a contact's list of email addresses in a table view, and allow the user to delete or modify each one. As far as I can tell, they need to be ordered so that I can tell which one is being removed/modified: for example, if the user taps the delete button in row 1, the second email address should be removed from the relationship. Without being ordered, I wouldn't know which one to remove.

When generating the NSManagedObject subclass, I get an NSOrderedSet property on the class, and CoreData helpfully provided some accessors in the .h file:

@interface Contact (CoreDataGeneratedAccessors)
- (void)insertObject:(TypedValue *)value inEmailsAtIndex:(NSUInteger)idx;
- (void)removeObjectFromEmailsAtIndex:(NSUInteger)idx;
- (void)insertEmails:(NSArray *)value atIndexes:(NSIndexSet *)indexes;
- (void)removeEmailsAtIndexes:(NSIndexSet *)indexes;
- (void)replaceObjectInEmailsAtIndex:(NSUInteger)idx withObject:(TypedValue *)value;
- (void)replaceEmailsAtIndexes:(NSIndexSet *)indexes withEmails:(NSArray *)values;
- (void)addEmailsObject:(TypedValue *)value;
- (void)removeEmailsObject:(TypedValue *)value;
- (void)addEmails:(NSOrderedSet *)values;
- (void)removeEmails:(NSOrderedSet *)values;
@end

My problem is that only some of these work. addEmailsObject: works just fine, but the remove or replace methods do not. If I try to replace one of the objects in the set with this call:

// newVal is a properly-built object requested from the managed object context
[self replaceObjectInEmailsAtIndex:index withObject:newVal];

I get a nasty error:

2013-08-16 16:51:19.687 AppName[43837:c07] -[Contact replaceObjectInEmailsAtIndex:withObject:]: unrecognized selector sent to instance 0xa56a800 2013-08-16 16:51:19.694 AppName[43837:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Contact replaceObjectInEmailsAtIndex:withObject:]: unrecognized selector sent to instance 0xa56a800'

From what I've read, this can happen if the object making the call (the Contact object, in this case) is created using regular alloc/init instead of being requested from the managed object context, but I'm doing that part correctly.

I also tried getting a mutable copy of the NSOrderedSet object, to make the updates and then replace the set:

NSMutableOrderedSet *newSet = [self.emails mutableCopy];
[newSet replaceObjectAtIndex:index withObject:newVal];
[self setEmails:newSet];

This didn't throw any errors, but did something wonky: my interface started displaying incorrect values and I'm not sure what happened.

So: what's the correct way to replace and remove objects, if not using the mutators that CoreData purports to provide?

Wain

The auto generated code has a bug which prevents it from working. Your workaround by creating a mutable copy, updating and setting that should work. Any issues in the UI that you see as a result should be a facet of how you're monitoring changes / reloading data to the UI.

Bug reference: here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I represent a one to many relationship with the Entity Framework?

From Dev

How to assign CoreData many-to-many relationship in Swift?

From Dev

How to handle ordered many-to-many relationship (association proxy) in Flask-Admin form?

From Dev

CoreData many to many relationship Best practices

From Dev

How do I enforce a unidirectional one-to-many relationship?

From Dev

How to use the “ANY” aggregate operation in a NSPredicate to filter a CoreData Many To Many Relationship

From Dev

coredata one to many relationship CoreDataGeneratedAccessors

From Dev

One to many relationship CoreData Swift

From Dev

How do I create a one to many relationship in Parse?

From Dev

CoreData: Query to one-to-many-to-many relationship

From Dev

How relationship works in coreData

From Dev

How to paginate a "has many" relationship that is ordered?

From Dev

EasyMapping do not map CoreData one to many relationship objects

From Dev

coreData insert to to many relationship

From Dev

Swift, CoreData and many-to-many-relationship: how to access order of subitems?

From Dev

In Many to One Relationship how to delete child objects

From Dev

How do I set up a many-to-many relationship in iOS? (Core Data)

From Dev

Grails: How do I fetch a sorted list of domain objects in a many-to-many relationship with sort attribute?

From Dev

How do I enforce a unidirectional one-to-many relationship?

From Dev

One to many relationship CoreData Swift

From Dev

CoreData :NSFetchedResultsController one to many to many to many relationship

From Dev

How do I have multiple has_many to has_many relationship between two models?

From Dev

How do I implement a unidirectional one to many self referencing relationship?

From Dev

CoreData - many-to-many relationship with priorities

From Dev

Django: How do I create a view to update multiple records on an intermediate model with a Many-to-Many-through relationship?

From Dev

coreData insert to to many relationship

From Dev

How do I establish a relationship between two objects where both need to have many of the other object?

From Dev

How do I define a one-to-one relationship over a one-to-many relationship in a relational database?

From Dev

How do I load/retrieve a Many to Many relationship in Firebase Realtime Database

Related Related

  1. 1

    How do I represent a one to many relationship with the Entity Framework?

  2. 2

    How to assign CoreData many-to-many relationship in Swift?

  3. 3

    How to handle ordered many-to-many relationship (association proxy) in Flask-Admin form?

  4. 4

    CoreData many to many relationship Best practices

  5. 5

    How do I enforce a unidirectional one-to-many relationship?

  6. 6

    How to use the “ANY” aggregate operation in a NSPredicate to filter a CoreData Many To Many Relationship

  7. 7

    coredata one to many relationship CoreDataGeneratedAccessors

  8. 8

    One to many relationship CoreData Swift

  9. 9

    How do I create a one to many relationship in Parse?

  10. 10

    CoreData: Query to one-to-many-to-many relationship

  11. 11

    How relationship works in coreData

  12. 12

    How to paginate a "has many" relationship that is ordered?

  13. 13

    EasyMapping do not map CoreData one to many relationship objects

  14. 14

    coreData insert to to many relationship

  15. 15

    Swift, CoreData and many-to-many-relationship: how to access order of subitems?

  16. 16

    In Many to One Relationship how to delete child objects

  17. 17

    How do I set up a many-to-many relationship in iOS? (Core Data)

  18. 18

    Grails: How do I fetch a sorted list of domain objects in a many-to-many relationship with sort attribute?

  19. 19

    How do I enforce a unidirectional one-to-many relationship?

  20. 20

    One to many relationship CoreData Swift

  21. 21

    CoreData :NSFetchedResultsController one to many to many to many relationship

  22. 22

    How do I have multiple has_many to has_many relationship between two models?

  23. 23

    How do I implement a unidirectional one to many self referencing relationship?

  24. 24

    CoreData - many-to-many relationship with priorities

  25. 25

    Django: How do I create a view to update multiple records on an intermediate model with a Many-to-Many-through relationship?

  26. 26

    coreData insert to to many relationship

  27. 27

    How do I establish a relationship between two objects where both need to have many of the other object?

  28. 28

    How do I define a one-to-one relationship over a one-to-many relationship in a relational database?

  29. 29

    How do I load/retrieve a Many to Many relationship in Firebase Realtime Database

HotTag

Archive