Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store images in sqlite or just a reference to it?

I have made couple of apps using coredata and I was storing images in sqlite, but somewhere i found that it is bad. I've searched the net but all I've found is this suggestion:

  • image size < 100kb store in the same table as the relevant data
  • image size < 1mb store in a separate table attached via a relationship to avoid loading unnecessarily
  • image size > 1mb store on disk and reference it inside of Core Data

So my question is: what are pros and cons of saving an image in sqlite db as NSData, and storing just a reference to the image while image is saved in the file system?

like image 557
Lukas Avatar asked Jan 23 '26 20:01

Lukas


1 Answers

Apple provide some guidance on this topic in their guide on Core Data Performance. In general, although SQLite scales pretty well and can handle databases that are many gigabytes in size with ease, large binary blobs are not queryable or indexable, and inflate the size of the database with little return.

If you're targeting iOS 4 and above, you can set the "Allows External Binary Data Storage" flag on your attributes that contain such data, and Core Data will automatically store them separately on the file system (if it deems appropriate), and automatically manage the link to that data in your data store.

like image 83
一二三 Avatar answered Jan 26 '26 12:01

一二三



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!