Fogmeister Fogmeister - 3 months ago 12
Objective-C Question

Magical Record cascade delete (truncateAll) not deleting

I'm using Magical Record in my app.

I have a relationship...

Competition <-->> League


i.e. A
Competition
can have many
Leagues
. A
League
can only have one
Competition
.

The delete rule from
Competition to League
is set to
Cascade
. The inverse delete rule is set to
Nullify
.

What I would expect to happen is if I delete a
Competition
then all
Leagues
that belong to it are deleted also. (I have used this in previous apps successfully).

I am testing by doing...


  1. Download and insert all competitions and leagues.

  2. Log all Leagues with Competition names.

    NSArray *leagues = [League findAll];
    for (League *league in *leagues)
    {
    NSLog(@"%@ - %@", league.name, league.competition.name);
    }

    e.g. Man UTD - Premier League

  3. run the following code...

    [Competition truncateAll];

  4. Log all Leagues with Competition names.

    e.g. Man UTD - (null)



I was expecting this to delete the
Leagues
as well as the
Competitions
?

Is my assumption correct in what it should do? Why isn't it doing it?

Answer

The relationship delete rules are applied when the managed object context is saved, or processPendingChanges is called.

As can be seen in the implementation, the MagicalRecord method MR_truncateAll calls deleteObject on all objects of an entity, but does not save the context.