22

I know that the storing of UIImage's in core data has been discussed a lot, such as here, but that was pre-ios5. Now that we have the external storage flag, do you guys think it would be a fine idea to store UIImage's directly in the entity, as a separate entity, or still on the disk?

Here is a source explaining the external storage option.

Community
  • 1
  • 1
Philippe Sabourin
  • 8,066
  • 3
  • 31
  • 46

1 Answers1

34

Core Data Release Notes for iOS 5.0

When enabled, Core Data heuristically decides on a per-value basis if it should save the data directly in the database or store a URI to a separate file which it manages for you. You cannot query based on the contents of a binary data property if you use this option.

And from your link External Binary Data, the heuristic seems to be

Objects that are smaller than 1MB are stored in the database. For objects that are larger, an external file is created and the database just stores a reference to it.

So the following advice is still valid: CoreData : store images to DB or not?

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

The flag sets Core Data to follow that advice and automatically store images >1MB as a separate disk file.

Community
  • 1
  • 1
Jano
  • 62,815
  • 21
  • 164
  • 192
  • 2
    So in that case we should probably put it in a separate entity in case it's in the 100kb-1mb range since the external storage doesn't account for that case? – Philippe Sabourin Oct 28 '11 at 13:52
  • 2
    Caveat: there is a bug with external image storage that makes it crash when deleted, sometimes: http://stackoverflow.com/questions/7930427/error-uiimage-deleteexternalreferencefrompermanentlocation-unrecognized-se – Philippe Sabourin Nov 22 '11 at 17:45
  • 5
    There is another bug with external storage and migration. If you try to create a new model and then use lightweight migration then you lose all the 'Binary Data' that are stored on an external file. – zirinisp Apr 26 '12 at 06:39
  • 2
    @zirinisp Any workarounds?? Im having that issue with an app, i have it live on the appstore and users stored images like that and i need to migrate. Can i use manual migration? – Nicolas S May 03 '12 at 05:38
  • can we access that url that is referenced for uploading image to other services? – alionthego Feb 03 '18 at 00:51