McDuck
McDuck

Reputation: 760

NSValidationMissingMandatoryPropertyError issue but all fields are populated

I have a two core data entities, An entity basket and an entity orange, and basket contains multiple oranges

basket
|-property A
|-property B
|-->oranges (NSSet)

oranges relationship in basket is set as non-optional, delete rule is cascade, type to-many.

orange
|-property A
|-property B
|-->basket

basket relationship in orange is set as non-optional, delete rule is deny, type to-One.

When I add one orange and save, there is no issues. When I delete all oranges and save it seems to be fine. (haven't had issues until now).

However when I delete one orange that is present in the basket by;

  [self.basket removeOrangesObject:orange];

I get NSValidationMissingMandatoryPropertyError

if I try by using the managed object and deleting through there I get NSValidationRelationshipDeniedDeleteError

When I debug through the orange there is no mandatory fields unpopulated (as it wouldn't be able to save it if there was).

I am starting to wonder if my relationships or type are wrong.

I have done extensive debugging and I cant seem to find an answer to this.

Upvotes: 0

Views: 80

Answers (1)

Paulw11
Paulw11

Reputation: 114975

This -

[self.basket removeOrangesObject:orange];

doesn't delete an orange, it removes the orange from the basket. The orange object will still exist, but it's basket relationship will be nil. However the basket relationship is non-optional so you can't have oranges that aren't in a basket.

If you want to delete the orange you should use

[managedObjectContext deleteObject:orange];

and you need to change the delete rule to for the orange->basket relationship to "nullify"

or you need to remove the orange from the basket as per your original code and then delete the orange before saving the managed object context.

Upvotes: 2

Related Questions