Joel Ollé Ripoll
Joel Ollé Ripoll

Reputation: 451

Core data with unique constraints and relationships-IOS

I have a core data design with multiple tables using relationships. My database is SQLite. For updates I import data from JSON and use this method:

[NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context]. 

I have added unique constraints in core data. If I update an entity that is a relationship of another object it loses the connection.

Ex: Entity "person" that contains the one to one relationship to "pet_id". If I update "pet" it changes his id and "person" still points to the old id, so they are not related any more. Is there a way to avoid this problem?

Upvotes: 0

Views: 1068

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70966

I don't think this is documented anywhere yet. Here's what it sounds like is happening:

  • You create a new instance. Your constraints mean that this instance matches an existing instance. But...
  • Your new instance has a nil value for this relationship. So...
  • The existing instance's value for the relationship is replaced by this new nil value.

To maintain the relationship, your new instance needs to already have the correct value for that relationship. You're essentially asking that the constraint matching system ignore the fact that the relationship value is different in your new instance, but to accept new values for other attributes.

I think what you're expecting is completely reasonable but I'm also not surprised that the current implementation doesn't support it. I recommend filing a bug with Apple about this, and investigating non-constraint based approaches to keeping your data unique.

Upvotes: 1

Related Questions