Deleting Cascade Relationship Objects in Master Data

I want to do some simple deletion with Core data, but you just need some tips on this.

I have a model with transactions, names, events, and date entities. A transaction has a connection with each of the other Entities.

In the application, when the user adds information to the text fields, he is saved in the table view controller with 4 tabs.

The first tab uses an NSFetchedResultController with fetchRequest in the transaction entity. The second tab uses the Entity Name, the third uses the Event, and the fourth uses the Date Entity.

If I delete an entry from the Transaction tab, it will delete that particular transaction, which makes sense.

What I want is to remove the person from the "Name" tab or the event from the "Event" tab and make it a cascade through the application model. Therefore, if Bob has several transactions, deleting him, the transaction tab will delete this transaction.

If I remove the BOB from the name tab, it must remove it from each event that was part with each date and transaction.

The same applies to events and dates.

EDIT: update to include data model

Note: Year Entity is experimental and not currently in use. enter image description here How can I do something like this?

thanks

+6
source share
2 answers

If you set a "Deletion Rule" for the "transaction" relationship from Person from Transaction to "Cascade", then deleting the user will automatically delete all related transactions.

+8
source

First, I do not see the wisdom of abstracting dates in essence. Maybe you can enlighten me. In my opinion, the date refers to the transaction. On the Date tab, you should still receive transactions, but imagine them grouped and sorted differently.

I suppose you know how to cascade relationships between people and transactions. This will delete all transactions related to the person. The same thing applies to the event.

Cascading in the other direction, however, is more problematic. You need to somehow check to see if he is the last member of a many relationship, and only delete it if that is the case. One way to do this is to override setters. In your NSManagedObject subclasses, you will find auto-generated setters, including for adding or removing objects from sets (i.e., To many).

 -(void)removeTransactionsObject:(Transaction *)value { [super removeTransactionsObject:value]; if (!self.transactions.count) { [self.managedObjectContext deleteObject:self]; } } 
0
source

All Articles