22

First time asking a question on here, so please go easy if I don't provide enough info. Basically part of my iOS app allows users to take a picture which will be stored in a Core Data store. The attribute is a Transformable type, and I have created an NSManagedObject subclass which I simply use to set its image attribute to the new image provided by the user.

I know storing large files in Core Data is a bad idea, which is why I was excited when I saw the "Store in External Record File" option under the image attribute in the Core Data entity. However, my app performance says otherwise, taking several seconds on an iPhone 5 to load only a few images (which I know doesn't sound like much time, but considering how powerful the iPhone 5 is, older devices would likely take much longer with the same data).

I've looked around, and some people say that the Store in External Record File option is only applicable to the OS X environment, even though it is available in an iOS app. However, I also saw this under Apple's "What's New in iOS 5" doc (it's the next to last item under Core Data, near the end):

Managed objects support two significant new features: ordered relationships, and external storage for attribute values. If you specify that the value of a managed object attribute may be stored as an external record, Core Data heuristically decides on a per-value basis whether it should save the data directly in the database or store a URL to a separate file that it manages for you.

So my question is, who's right? Is it true that Apple made a mistake in giving this option for iOS apps, and that it actually does nothing unless you're on the Mac, or does it actually do something and I'm not configuring it the right way, or is it doing what it's supposed to do and the performance is bad anyway?

I've seen some guides explaining how to store large files (like images) as files, and save the URL to them in the Core Data store instead, but since this is essentially what this new option is doing, or maybe should be doing, I'm not sure if following these guides would even help.

I'm really sorry if this has been asked before. Normally I'd be fine with figuring this out on my own, but Core Data is totally new to me, and I'm still not sure how I managed to squeak by the initial setup. Thank you for any help you can offer!

1
  • Don't be sorry, this is a valid question. +1 to the question.
    – svena
    Apr 15, 2013 at 6:47

2 Answers 2

8

who's right ?

the iOS docset for the NSAttributeDescription class does mention the allowsExternalBinaryDataStorage and the setAllowsExternalBinaryDataStorage: methods so there is little chance that there is a mistake from Apple.

are you doing something wrong or is slow anyway ?

You said that

The attribute is a Transformable type

But Core Data has a Binary data type. Maybe only this one is linked to the external storage capability.

if that's not it, we don't have enough info here:

  • How many pictures do you store ?
  • What are their sizes ? 
  • Do you automatically fetch all the images ?

Also, the Apple doc states that:

Core Data heuristically decides on a per-value basis…

  • Did you use a migration or are you starting from scratch ?

You could have a look in your app's sandbox to see if your pictures are really saved outside of CoreData.

Hope this helps.

1
  • 5
    Thanks, you got it with the data type. I was originally using the Transformable type, giving it a plain UIImage, but when I switched to the Binary Data type, giving it an NSData object of the image, as you suggested, it revealed the option "Allows External Storage" under the attribute type in the inspector, which is the setting I was after. I also tested this by un-checking it and logging how long it took to load and save the images vs when it was checked, and it was way faster when it was checked. Thanks for your help! :)
    – kingsapo
    May 19, 2013 at 2:05
3

Good question!

Check this post: Storing blobs in external location using built-in CoreData option

Apparently it should work. You should also try it in the simulator and inspect the application data folder to see if the folders are created as described (~/Library/Application Support/iPhone Simulator/... - you will figure out the rest of the path). Also you could inspect the sqlite file with the sqlite3 command to see if the binary data is in the database.

I haven't personally used this option as I would prefer to go for manually saving the images in a folder and store a reference to them in the database instead. This way it will be easier to create UIImage object from the file to be displayed, would have better control on what goes where and so on and so forth. Will take some extra labour though!

Hope that helps you out.

1
  • 1
    Thanks for your comment! Your answer was correct, but since teriiehina's explained about the Binary data type, I marked his as correct. At one point I did attempt to manually save my images, and it seemed to be working well, but it got difficult when I started trying to delete images. There was some issue where I could never get the delete method to get called, but I was probably doing it wrong - I don't know much about Core Data. Still, doing it manually is a great suggestion for someone more experienced, so thanks! :)
    – kingsapo
    May 19, 2013 at 2:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.