Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
    }
like image 957
Marc Nunes Avatar asked Dec 03 '25 23:12

Marc Nunes


1 Answers

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.

like image 111
Matthias Bauch Avatar answered Dec 05 '25 13:12

Matthias Bauch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!