6
votes

In my Core Data model (on iPhone SDK 3.1) I have several entities that are associated with the same instance of an image. The image itself is also stored as managed object. In order to save disk space, I'm trying to create 1 db file for the images and another db file for all other objects.

Reading Apple's documentation and googling for days I came to the following conclusion: - Create 1 datamodel that contains the description of all entities - Use 1 persistent store coordinator

  • Use configurations to store image entities in a different file (sqlite) than the other entities (see here)

  • And, as Core Data does not support relationships from instances in one persistent store to instances in another persistent store, use "Fetched Properties" to create weak, one-way relationships

Using configurations is pretty straightforward: assign a configuration to the entity using the data modeler and declare which configuration to use when adding a persistent store to the coordinator. In my case, I added to stores to the coordinator.

But here's the problem: when fetching, let's say 'User' objects from one store, how do I use fetched properties to load the images associated with that user (and assign them to the user object)?

Do I have to write a custom accessor method for 'images' in the user object? How (and where) to specify the predicate for the fetched property?

I was convinced there must be a tutorial or example explaining this (as this obviously is the way Apple suggests to do it). But no luck so far.

Any idea?

Hopefully we can start a discussion on this topic. I'm sure there are other (more clever) ways to solve this...

2

2 Answers

1
votes

Using separate stores will not make anything "better" in this situation. Storing large binary data in Core Data is not recommended. You should store it on disk and use references to the file system.

See the BLOBs section here.

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdPerformance.html

1
votes

I am not sure that your assertion about disk space is correct. Without evidence from testing your app, I would stick with the simpler, one store, solution. That said...

You need to store some identifying attribute of the image(s) in the User class. I would use a transformable property to store the manager object IDs. You add a fetched property in the data modeler, and specify the predicate there as well.