CoreData (for iphone) storing images

后端 未结 4 688
旧时难觅i
旧时难觅i 2020-12-08 08:55

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 pro

4条回答
  •  借酒劲吻你
    2020-12-08 09:13

    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.
        }
    }
    

提交回复
热议问题