0

I'm using CoreData as my local app storage manager.

Sometimes I save BLOB files (images, videos, etc) and I notice that the app size increases, which is expected.

My problem appears when I delete some data, but the app size doesn't change.

I've downloaded the app's container and noticed that the appname.sqlite and appname.sqlite-wal are still large.

Does anyone know why this is happening?

Note: I'm using CoreData with CloudKit with NSPersistentCloudKitContainer if that can help.

Ivan Cantarino
  • 3,058
  • 4
  • 34
  • 73
  • 2
    I don’t know the answer, but storing big binary blogs in core data is a really bad idea. Use ordinary files and store the filenames. – matt May 12 '20 at 23:31
  • See sqlite vacuum for an explanation of what is happening: https://www.sqlite.org/lang_vacuum.html -- but probably just do what @matt said – Lou Franco May 12 '20 at 23:36
  • @matt thanks for the reply. Just wondering, since this is an optional feature, that needs to be synced throughout the users iOS devices, thus the NSPersistentCloudKitContainer, what would be a good way to do this? Save it in the iCloud Drive, then store the URL in the CoreData model? – Ivan Cantarino May 13 '20 at 06:37

1 Answers1

2

Under CoreData is a sqlite database. Sqlite's vacuum command documentation explains what is happening

https://www.sqlite.org/lang_vacuum.html

When content is deleted from an SQLite database, the content is not usually erased but rather the space used to hold the content is marked as being available for reuse.

You could try to connect to the underlying sqlite database and use vacuum directly on it.

How to VACUUM a Core Data SQLite db?

But, in your case, I'd follow @matt's suggestion of not using BLOBs this way.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192