i wonder if its a wise choice to store images with core data into binary property
say i have a collection of movies and i want to save the image dvd cover into a property the avg size of a cover is 20/30kb (320x480px)
the reason i want to do this is for storage management, once i delete the movie i know the image is also deleted
i'm just not quite sure if it's a good idea, data load, speed?
anyone has experience with this ?
Yes, we can store the images with the core data but not directly as UIImage. This can be achieved by converting UIImage to Data. Because UIImage is not a valid attribute type in Core Data. If you are new to the core data and want to learn from the basics then refer to this post.
Saving the Image in Core Data In the file, we have created one class , DataBaseHelper , and then we have one singleton of class DataBaseHelper to access the properties and methods of this class. Next, we create context using UIApplication , then we have one method, saveImage() , to save the user-picked image.
Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.
Right-click on your project's folder in the project navigator and then New File… In the new window, type “data” in the top right corner, select Data Model, and press Next. Give it a name, and save it. Now, let's add all the necessary code to connect Core Data with our project.
It seems to me that storing images in a core data isn't a good idea, and I'm pretty sure I've also read it on one of Apple's programming guides. Just consider this, when you fetch your collection, all the images will also be loaded into memory, even if you're only displaying 8 images, your entire image collection will be loaded by core data.
If you want to make sure you delete the image files when a move record was deleted I suggest you listen to notifications by the managedObjectContext and delete files when a movie was deleted:
You can either register for the willSave or didSave notification, each with it's own advantages/disadvantages.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextDidSave:) name:NSManagedObjectContextDidSaveNotification object:managedObjectContext];
Getting the deleted objects:
- (void) contextDidSave:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
for (NSManagedObject *currObject in [userInfo objectForKey:NSDeletedObjectsKey])
{
// Code for deleting file associated with the NSManagedObject, you can still
// access the managed object's properties.
}
}
The best method is to store the path to the image in the managed object and then delete the file in the object's prepareForDeletion
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With