App increases size even if I delete data (Core Data)

Hello

I noticed that in my app, when I add data (I am using Core Data) the size of the app increases, as expected, the problem occurs when I delete data, the size of the app remains unchanged and sometimes increases, I thought there was an error in the my code and so I created, from scratch, a project for iOS with SwiftUI and Core Data enabled (the default template that Xcode provides) and also with the SwiftUI & Core Data default app the same problem happens.

Is there a way to fix it or is there an explanation for this?

Thank you!

Answered by deeje in 685706022

short answer: don't worry about it. Apple has invested heavily in CoreData and uses it extensively. wrt deleted records, Core Data will clean up its storage over time.

longer answer: to understand what's going on, you'll have to dive into the architecture and implementation of structured database systems, and specifically how SQLite (which is used by default by Core Data) organizes data and indices into its file structures. For some insights, see here https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreData/PersistentStoreFeatures.html

when I delete data, the size of the app remains unchanged and sometimes increases,

AFAIK, deleting data does not mean that you purge the space and compact the database.

You should probably have to recreate the database completely, just keeping existing records.

In any case, does this cause you problem or is this for understanding ?

Note: may be interested in reading this: https://stackoverflow.com/questions/64677236/how-to-check-the-size-of-coredata-in-swift

You should probably have to recreate the database completely, just keeping existing records.

If you can explain better your solution it would be better for me

In any case, does this cause you problem or is this for understanding ?

It does not create me a direct problem, however I want to find a solution, because if you think about it, if you add and delete data every day for 100 days, even if there is no data (in the app) there will be a lot of MB occupied by the app in the iPhone Storage.

Thank You!

Accepted Answer

short answer: don't worry about it. Apple has invested heavily in CoreData and uses it extensively. wrt deleted records, Core Data will clean up its storage over time.

longer answer: to understand what's going on, you'll have to dive into the architecture and implementation of structured database systems, and specifically how SQLite (which is used by default by Core Data) organizes data and indices into its file structures. For some insights, see here https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreData/PersistentStoreFeatures.html

App increases size even if I delete data (Core Data)
 
 
Q