1

I'm having trouble with a core data many-to-many relationship where both side's delete rules are set to Nullify. I'm finding when I inspect the SQL database there are records left in the join table that should be deleted.

The two tables represent Playlists and Tracks. Playlist deletes don't cascade to delete each track automatically because each track can be in multiple playlists.

The tracks in a playlists are in an ordered relationship.

The track is being removed from the playlist and separately the playlist and track are being deleted but there remains a record in the Z_6PLAYLIST_TRACKS table in the database.

How do I make sure these records are deleted too?

1 Answer 1

0

Most likely the full delete has not propagated into the sqlite file and is still sitting in one of the two cache files. This is common now that sqlite is being used with WAL mode by Core Data.

As an aside, I would not worry about the tables inside of Core Data like that. Access Core Data through the public API only. The internal workings of the SQLite file is not something you should be trying to keep in sync.

If you are getting the data you expect to be getting and your deletes are giving you the result you expected then that is sufficient. Only if you are having issues with your deletes should you be working about the internal structure of the SQLite file.

Update 1

Thanks for the test case, that is an actual issue in Core Data. There is nothing in there that you are doing incorrectly. You REALLY need to file a radar on this and I will file one as well.

In the interim you can either stop using ordered relationships (they are a bastardization anyway) or do the double delete.

Personally I would change it from ordered relationships as they have terrible performance and clearly have issues.

6
  • Thanks for your reply Marcus. The records in Z_6PLAYLIST_TRACKS are definitely staying around and not being deleted. This problem was noticed with a user who had accumulated a large database and thousands of these records had been left behind. I tried disabling WAL mode and the issue remains. I've created a test project and see the same problem that if I link playlists and tracks, then call deleteObject: on the playlist and track the relationship record remains in the database.
    – Tonester
    Dec 8, 2015 at 11:44
  • I've noticed if you delete the relationship i.e. using removePlaylistTracksObject:, then save, then delete the playlist and track, then save, it works. I would expect the relationship record to be removed when deleting either the playlist or track and a single save. Also if the playlist to tracks is not ordered it works fine. I think this highlights the issue. Apple's implementation of ordered relationships has a known bug (stackoverflow.com/questions/7385439/…) and I had to override the default handling.
    – Tonester
    Dec 8, 2015 at 11:44
  • I thought this was unrelated before but maybe it isn't.
    – Tonester
    Dec 8, 2015 at 11:44
  • Ahh, I missed the ordered relationship part of the question. That lends more weight to it being an issue in the API. Since you have a test case can you post it here and/or file a radar on the issue? Dec 8, 2015 at 20:46
  • What effect is the user seeing from this issue? Dec 8, 2015 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.