Reputation: 151
I have some simple code that saves an image (and some other values) stored in an NSMutableDictionary and the image's thumbnail to CoreData:
self.imageInfo.lastSaved = [NSDate date];
self.imageInfo.thumbnail = UIImageJPEGRepresentation([self imageThumbnail], JPEGCompressionQuality);
if (!self.imageInfo.details)
{
self.imageInfo.details = [NSEntityDescription insertNewObjectForEntityForName:@"ImageDetails" inManagedObjectContext:[self managedObjectContext]];
}
self.imageInfo.details.values = [self imageValues];
NSError *error;
[[self managedObjectContext] save:&error];
I have verified the image is 2.1 MB (and the thumbnail is only 11 KB). All other data is just CGFloats or short strings. When I save the first time, everything seems to be OK when I look at the database files in Documents and Directory:
However when I save again without changing a thing, I get:
I have verified that the image size and thumbnail size are staying the same. After five saves I get:
I don't understand the fluctuation in sizes when I'm not changing a thing. It appears the database has quadrupled in size after the second save, and then goes back to only doubling in size after the first 5 saves. I don't understand what is happening here.
UPDATE: In analyzing the database using sqlite3_analyzer, I see that the payload is the correct size, but yet the size of the sqlite file is exactly double the amount of the payload after 5 saves. Is this standard behavior? Is it creating unnecessary empty space for some reason?
Upvotes: 1
Views: 862
Reputation:
It is hard to answer on Why questions. Did you see any particular problem using Core Data? A singular memory or file size increase as described in your post is neither a failure nor a problem, but if memory or file size would increase continuously for no obvious reason it is a failure. If you are in doubt you could check if Core Data, your App code and SQLite works correctly: open the SQLite file with an SQLite client and enter the VACUUM
command. Close the SQLite client and check the file size, then repeat the Core Data commands (but don’t add new data) in your app and again enter VACUUM
through the SQLite client. File size should not have been changed.
Upvotes: 1