user523234
user523234

Reputation: 14834

One to Many remove Object not deleting object on many entity

I have one entity called Activity (one) and Game (many). I setup the delete rule as Cascade for the "game" relationships on the Activity entity and Nullify (*** more on this later) for the "activity" relationships on the Game entity.

If I delete an Activity object, then all Game objects would also be deleted from core data. So the cascade rule for that relationship seemed to be working fine.

Now, if I delete one or all of the the many objects "Game" using either provided methods: removeGameObject: and removeGame:, that Activity object would no longer be linked to any Game objects. So that seemed to work. But upon doing an independent query the "Game" entity, those supposed to be removed Game objects were still in Core Data. They are not just being linked to any Activity object.

*** I have also tried No Action, Cascade deleting rule as well.

I can manually remove the Game object(s). But there must be a right way to do this.

Any pointers?

@class Game;

@interface Activity : NSManagedObject

@property (nonatomic, retain) NSNumber * activityID;
@property (nonatomic, retain) NSString * activityType;
@property (nonatomic, retain) NSSet *game;
@end

@interface Activity (CoreDataGeneratedAccessors)

- (void)addGameObject:(Game *)value;
- (void)removeGameObject:(Game *)value;
- (void)addGame:(NSSet *)values;
- (void)removeGame:(NSSet *)values;

@end

@class Activity;

@interface Game : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) Activity *activity;

@end

Upvotes: 0

Views: 718

Answers (1)

Paulw11
Paulw11

Reputation: 114975

removeGame and removeGameObject are methods on the Activity entity, so why would you think that they would delete a Game object? They do exactly what they say, the remove the Game from this Activity - removing the reference, but they don't actually delete the Game object. These methods are a little more useful where you have a to-many rather than a to-one relationship as they allow you to easily remove a particular object from the relationship.

If you want to delete the Game entity then you need to call the deleteObject: method on your NSManagedObjectContext instance, specifying the Game that you want to delete. If you do this and you have set the delete rule to Nullify on the inverse relationship from Game to Activity then the reference to the Game will be removed from the Activity.

Upvotes: 4

Related Questions