views:

47

answers:

2

Hi All,

I have an iPhone Core Data app with a pre-populated sqlite "baseline" database. Can I add a second smaller sqlite database with the same tables as my pre-populated "baseline" database but with additional / complementary data such that Core Data will happily union the data from both databases and, ultimately, present to me as if it was all a single data source?

Idea that I had is:

1) the "baseline" database never changes.

2) I can download the smaller "complementary" sqlite database for additional data as and when I need to (I'm assuming downloading sqlite database is allowed, please comment if otherwise).

3) Core Data is then able to union data from 1 & 2. I can then reference this unified data by calling my defined Core Data managed object model.

Hope this makes sense.

Thanks in advance.

A: 

You can attach the downloaded database with ATTACH DATABASE statement and operate with unions of tables.

newtover
This question is with regard to Core Data; not raw SQLite.
Marcus S. Zarra
@Marcus S. Zarra: thanks for the comment. Do not mind my answer then =)
newtover
+1  A: 

Core Data is designed to handle multiple data files via the – addPersistentStoreWithType:configuration:URL:options:error: method. This will allow you to combine all of the data files together and then access them via a single NSManagedObjectContext.

Your only issue, and it may not even be an issue for you, is that the store files cannot directly reference each other. Therefore you will need to reference data between files "manually" via unique identifiers. However I suspect you are already aware of that limitation.

Manual Relationships

The idea is that when both objects in a "relationship" are in one model and one file, Core Data does its magic and handles all of the referential integrity for you. However when they are in different files and/or models this doesn't happen automatically anymore.

The solution to this issue is to use a fetched property that looks up some unique identifier to retrieve the entity (or entities) that you want to be on the other side of the relationship. This will give you a "weak" relationship between files.

One thing to note though when doing this. The fetched property does not get updated automatically when something changes. This means when data changes that would cause that relationship to change, your application will not be automatically made aware of it and you will need to request that property again to get the updated relationship information.

Hopefully that makes it a bit clearer.

Co-existance of fetched properties and relationships

They can definitely co-exist but realize that they are two separate properties. If you want your controller code to see them as one, then I would suggest building a subclass for that entity and then adding a convenience method in there that hits both the relationship and the fetched property and then rolls them up into one NSArray or NSSet before returning it back to your controller code.

Marcus S. Zarra
Wow, that sounds absolutely awesome! Let me try this out. The more I learn about Core Data, the more I realise its seriously cool stuff. BTW - Marcus, you rock, you've been answering a number of my Core Data-related questions and I totally appreciate your help and expertise.
Tofrizer
Hi Marcus - can you please expand on the "reference data between files 'manually' comment"? I have entities with relationships to each other and one challenge (which I believe you are alluding to) is for an instance of entity 1 in sqlite store1 to work with its relationship to (an instance of) entity 2 in sqlite store2? How do I "manually" manage this? Thanks
Tofrizer
Thanks - I see, let me read up on fetched properties. Can fetched properties co-exist with relationships between the entities? E.g., 1) on my baseline database, Core Data magic happens thanks to the relationships between two objects on this store. 2) For data/instances of the objects across two different stores, instead of using their relationship, if I understand correctly I need to use fetched properties. Can 1) and 2) co-exist?
Tofrizer
Ok cool thanks a lot.
Tofrizer