10

I am curious to know who uses multiple core data models and why, what are the benefits, I am developing an application which I think I could benefit from multiple models, but I'm unsure of other benefits.

My up and coming app will be for iPad and another version for iPhone, the iPad has 3 main contents, the iPhone only has one which is also on iPad, so by isolating this into a different core data model maybe maintaining changes of that model across my two apps will be easier.

Although I do have a couple of entities which I would need in both, so I could just copy them over or have one big model.

Any suggestions ?

Anyway this isn't of course a common situation, what other scenarios might lead you to create multiple Core Data models ?

Daniel
  • 23,129
  • 12
  • 109
  • 154

2 Answers2

10

Sometimes it makes sense to keep different kinds of data in different stores. For example, an app that works like a product catalog might have one store that's a product database, and another that keeps track of the user's favorites, current orders, and history. That makes it relatively easy to update the product database without affecting the user's data, and to back up the user's data without having to copy the entire product database.

Another scenario where you'd use multiple stores is to store the same type of data. Document-based applications, for example, will generally create a separate store for each document -- the store may be the document.

Update: What I wrote above addresses using separate stores, but you asked about using separate models. Core Data will actually let you define separate models and then merge them all together at runtime for use in the same store (or multiple stores, for that matter). So, just to be clear, a model defines entities and the relationships between them. A store is the place where the data is actually saved using the schema defined in the model. You might break a complicated model up into several smaller models just to keep things simple and to aid in migrating your data as you modify your models over time, or you might use multiple models and keep them separate because you plan on using different stores that contain different types of data, as described above.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • By adding favourites in model A would you not be referencing an object in model B (product model)? – Daniel Apr 29 '12 at 19:02
  • @Daniel Right -- you can't create relationships between objects in different *stores*, so you'd generally save enough information (e.g. a product code) that you can find the object you want again. See my update for the important distinction between stores and models. If the objects in question are all going to be kept in the same store, and if you need to create relationships between them, then you should go ahead and use one model. – Caleb Apr 29 '12 at 19:14
3

I'd recommend using just 1 core data model. If you separate them, you won't be able to use a lot of the features of Core Data, such as relationships (between objects in the data store) etc. Even if you don't see the need right now, you may come up with an idea to add to the app later that needs it.

You could still use the same core data model for both iPad and iPhone, just ignore the parts you're not using the iPhone (until you get feature requests to add the missing parts, which you very likely will). Then you'll be all set and already have the data available.

Only in extreme cases would it be worth using a separate data model, for example, if you were going to download an existing data set that was read only etc. You might separate the read only data set from the users settings/data etc.

Good luck with the app!

Dave Wood
  • 13,143
  • 2
  • 59
  • 67
  • To point out the obvious, you can certainly use relationships when you use multiple stores; you just can't create relationships between objects in different stores. I know that's what you meant, but as described above the situation sounds worse than it really is. – Caleb Apr 29 '12 at 18:57
  • So in my case I do have some entities to share, Media being one of them. I have a MediaVideo, MediaImage and MediaFile which hold remote uris and deal with local caching of the actual file, I would want to reuse these and couldn't do so with multiple models if I understand correctly – Daniel Apr 29 '12 at 19:02
  • 2
    What other features would you include in "a lot of the features of Core Data"? IMO, the fact that you can have multiple stores and access them together fairly seamlessly is itself a HUGE feature of Core Data. – Caleb Apr 29 '12 at 19:03
  • @DaveWood: Bit late in the day but thanks for this. You've just helped me make a decision and I'll be going with one data model for now. – Robert Jul 23 '13 at 13:36
  • 1
    It's worth noting that if you are using multiple data models, that it appears impossible to do a migration when updating one of them, because the resulting model used by the app is merged from all. – ray Aug 26 '13 at 16:57