Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing images in Core Data or as file?

I have set of data which contains images also. I want to cache this data. Should i store them on file system or on core data and why?

like image 967
Abhinav Avatar asked Nov 11 '10 19:11

Abhinav


People also ask

What is transformable in core data?

When you declare a property as Transformable Core Data converts your custom data type into binary Data when it is saved to the persistent store and converts it back to your custom data type when fetched from the store. It does this through a value transformer.

What is core data stack in Swift?

As I mentioned earlier, the Core Data stack is the heart of Core Data. It's a collection of objects that make Core Data tick. The key objects of the stack are the managed object model, the persistent store coordinator, and one or more managed object contexts.


2 Answers

There are two main options:

  1. Store the file on disk, and then store the path to the image in core data
  2. Store the binary data of the image in core data

I personally prefer the 1st option, since it allows me to choose when I want to load the actual image in memory. It also means that I don't have to remember what format the raw data is in; I can just use the path to alloc/init a new UIImage object.

like image 95
Dave DeLong Avatar answered Oct 09 '22 07:10

Dave DeLong


You might want to read this from the Core Data Programming Guide on how to deal with binary large objects (BLOBs). There are rules of thumb for what size binary data should and should not be stored within the actual Core Data store.

You might also look at Core Data iPad/iPhone BLOBS vs File system for 20k PDFs

If you do place binary data within Core Data store, you would do well to have a "Data" entity that holds the actual data and to have your "Image" entity separate. Create a relationship between the two entities, so that "Data" need only be loaded when actually needed. The "Image" entity can hold the meta-data such as title, data type, etc.

like image 33
westsider Avatar answered Oct 09 '22 07:10

westsider