Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data Cross-Store Relationship with Fetched Properties

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...

like image 564
Philipp Bolliger Avatar asked Jun 10 '10 13:06

Philipp Bolliger


People also ask

What is fetched property in Core Data?

Fetched Properties in Core Data are properties that return an array value from a predicate. A fetched property predicate is a Core Data query that evaluates to an array of results.

What are relationships in Core Data?

Inverse relationships enable Core Data to propagate change in both directions when an instance of either the source or destination type changes. Every relationship must have an inverse. When creating relationships in the Graph editor, you add inverse relationships between entities in a single step.

What class would you use to filter fetched results from Core Data?

You can use . filter on the fetchedResults. Yes you can, but that's a Swift function. Using NSDelegate delegates filtering to Core Data which can be more efficient, especially on large data sets.


1 Answers

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

like image 99
logancautrell Avatar answered Sep 30 '22 16:09

logancautrell