Search code examples
ioscore-dataicloud

Core Data deleting External Storage entity doesn't free up space in iCloud


In my app I have this simple note <-->> photo relationship in Core Data sqlite, with the photo being the Binary Data with "Allow External Storage", and the deleting rule is note cascade photo, photo nullify note. When I tried to add some notes with photos then delete them, it looks fine in iPhone Simulator directory (the external photo files are deleted). However, when I tried it in my iPhone with iCloud sync, the iCloud storage size of my app only keeps increasing whenever I add photos, but never gets reduced after I delete notes or photos - any ideas?


Solution

  • You deleted the photo, but the transaction log is still there. Core Data's iCloud integration is based on transactions-- any time you save changes, a new transaction log is created. Transactions cover creating, updating, or deleting data. A transaction log sticks around until the underlying iCloud software decides to do something about it.

    When you create a photo, you get a transaction log that creates the object and contains the photo data. When you delete the photo, you get a transaction log that says to delete that photo. The second log does not cancel out the first one, it adds to it. Now you have both transaction logs, and the photo data is still there.

    At some point, the underlying iCloud code will coalesce the logs, and at that point the photo should actually disappear. You can't make that happen, though, you have to wait until it happens for you at some future date.