views:

52

answers:

1

In the app I am working on now I was storing about 500 images in Core Data. I have since pulled those images out and store them in the file system now, but in the process I found that the app would crash on the device if I had an array of 500 objects with image data in them. An array with 500 object ids with the image data in those objects worked fine. The 500 objects without the image data also worked fine. I found that I got the best performance with both an array of object ids and image data stored on the filesystem instead of in core data.

The conclusion I came to was that if I had an object in an array that told Core Data I was "using" that object and Core Data would hold on to the data. Is this correct?

A: 

Short answer is yes.

The long answer is that it depends on the size of the images. The rule is:

  • less than 100kb store it in a main table.
  • less than 1mb store it in a secondary table on the other end of a relationship.
  • greater than 1mb, store it on disk and reference it via a file path.

So, depending on the size of your files will determine where to store them. Also, keep in mind that UIImage handles caching so you may not need to store the images in an array at all.

Update

Your question is unclear then. You do not need to store the images in an array because A) the image is being held by the cell and; B) UIImage will cache the image for you so it will not be retrieved from disk if it has been accessed lately. So you are forcing the retention of images unnecessarily.

With regard to Core Data itself, it will drop attributes out of memory as needed as well. It will automatically pull them back into memory when accessed. Core Data also caches the data so you should not see any performance issues there either as things are being moved around in memory.

Marcus S. Zarra
The images were all in the 1mb-2mb range and I already pulled them out. My question is more about Core Data and what causes it to hold something in memory vs dumping it and reading from the database later.The array is because I have the images in a paged scrollview and I keep 1 image in both directions preloaded.
Kevin