Deleting Relationship Objects with Cascade in Core Data
Asked Answered
N

2

6

I'm looking to perform some simple deletion with Core data but just need a bit of advice on this one please.

I have a model with Transaction, Name, Event and Date Entities. The Transaction has a link to each of the other Entities.

In the app, when a user adds in information to text fields, that gets saved to a 4 tab table view controller.

The first tab is using NSFetchedResultController with a fetchRequest on the Transaction Entity. The second tab is using the Name Entity, the third is using the Event and the fourth is using the Date Entity.

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

What I want is to be able to delete a person from the Name tab, or an event from the Event tab and have that cascading through the model of the app. So if Bob has multiple transactions, deleting him the transaction tab will delete that one transaction.

If I delete BOB from the name tab, it should deleting him from every event that he was part off with every date and Transaction.

The same applies for the events and dates.

EDIT: Update to include data model

Note: Year Entity is experimental and currently not being used enter image description here How would I go about doing something like this?

Thanks

Nedi answered 11/12, 2013 at 19:16 Comment(2)
It would help if you post a screenshot of your Core Data model.Reveal
Thanks Martin - updated question to include modelNedi
R
8

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

Reveal answered 11/12, 2013 at 20:2 Comment(3)
With this configuration what about deleting all Person's transactions without deleting the person? Would setting transactions to nil or emptying it be enough?Joni
@Rivera: No. The "Deletion Rules" are only applied if an object is deleted.Reveal
@MartinR If a Transaction entity has been deleted and there is a cascade relationship from transaction to person. Will the Transaction entity is removed from the person transaction set ?Breslau
V
0

First, I cannot see the wisdom of abstracting dates into entities. Maybe you can enlighten me. In my mind, the date belongs to the transaction. On the date tab you still have to fetch transactions but present them grouped and sorted differently.

I suppose you know how to make your Person-to-Transactions relationships cascade. This will remove all transactions associated with a person. The same applies to Event.

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

-(void)removeTransactionsObject:(Transaction *)value {
    [super removeTransactionsObject:value];
    if (!self.transactions.count) {
       [self.managedObjectContext deleteObject:self];
    }
}
Vicarage answered 11/12, 2013 at 20:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.