Marc Nunes
Marc Nunes

Reputation: 238

CoreData not removing objects one-to-many entities

enter image description here

this will delete objects in BLOG but not in LABEL. I thought coredata would delete in both intities automatically? Do I have to do it manually on each entity? How do I do that LABEL doesn't have a remove method.

    //delete records from database no longer needed
    for (NSManagedObject *blogTBD in deleteArray) {

        //Delete object from BLOG
        [self.managedObjectContext deleteObject: blogTBD];
     }

also in my BLOG.h file there are four methods. Can someone explain why they are used for since I can't seem to use them for anything useful.

- (void)addLabelsObject:(LABEL *)value;
- (void)removeLabelsObject:(LABEL *)value;
- (void)addLabels:(NSSet *)values;
- (void)removeLabels:(NSSet *)values;

In my head the logic would be [blogObj addLabels: nssetoflabels] to add new labels in LABEL or [blogObj removeLabelsObject: label] to delete labels in LABEL but none of that works.

Here is how I'm adding labels to LABEL for each blog, it's the only way I got it to work:

  for (int i = 0; i < newCategory.count; i++) {

        LABEL *blogLabels = [NSEntityDescription       insertNewObjectForEntityForName:@"LABEL" inManagedObjectContext:self.managedObjectContext];
        blogLabels.categories = [NSString stringWithFormat:@"%@", newCategory[i]];

        //Assign relatioship - add labels to blog
        blogLabels.blog_labels = myBlog;

        //save label
        [self.managedObjectContext save:&error];
    }

Upvotes: 3

Views: 1234

Answers (1)

Matthias Bauch
Matthias Bauch

Reputation: 90117

You have to change the delete rule in your model from "Nullify" to "Cascade".

enter image description here

Nullify is the default, because it's the safest. Nullify doesn't delete anything, it just sets the inverse relationship to Null.
Cascade will delete the object(s) at the destination of the relationship.


regarding your second question, those methods work exactly like you think they do.

Why they don't work? I have no idea. They should work.

Upvotes: 5

Related Questions