开发者

CoreData (for iphone) storing images

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开发者_如何学Go 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 ?


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.


It occurred to me that you can use another approach which might be better if you're already subclassing NSManagedObject for your movie entity.

You can implement the didSave or willSave method on the NSManagedObject and check isDeleted property. If it is deleted, delete the image from disk.


See my answer in:

Can I access the files used for external binary storage in Core Data?

You can store large files in Core Data now using the 'Allows External Storage' option. The only problem with this is no longer having access to the actual files by path anymore, only the pure binary data. I came up with a 'creative' solution (whether it best practice is anyones guess), but it will allow you to use Core Data to manage your files while still being able to access the REAL files by their path.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜