2

I have a doubt about Core Data migration.

Say I have an application which has some predefined values in a table A. I want to sync it with another database, with a table B in such a way that when new records are added totable B, that record should get added to my table A.

I know using Core Data migration, when I add columns to a table, I will be able to access the values previously stored in the older table before the addition of the column.

I would like to know how my table can be updated with the added records on another table.

Update:

From comment below:

The question I had in mind is this... I want to release an update for my app. I'm stuck on how to update the existing Core Data database which also stores data entered by the user. All I need to do is update a couple of records and preserve current user data. How do I do this?

1 Answer 1

5

Core Data is not SQL. Entities are not tables. Objects are not rows. Columns are not attributes. Core Data is an object graph management system that may or may not persist the object graph and may or may not use SQL far behind the scenes to do so. Trying to think of Core Data in SQL terms will cause you to completely misunderstand Core Data and result in much grief and wasted time.

That way lies madness.

It sounds like you don't actually want to migrate as the term is used in Core Data. Migration in Core Data means moving from an earlier version of a data graph's persistent store to a newer version of the same.

E.g. In the 1.0 version you have an entity Person with the attributes firstNameand lastName. After the app has been release you wish to update to the 2.0 version and add a phoneNumber attribute to the Person entity. You would use migration to update the user's existing object graphs and persistent stores to the new object graph.

If by "table" you actually mean entities, then you can link entities together in a relationship so that they can watch each other. If by "table" you mean a data model or persistent store, then the answer is more complex. It can be done using configurations, fetched attributes, UUIDs etc but you must understand what you really need to do before you jump through all those hoops.

Sign up to request clarification or add additional context in comments.

4 Comments

@TechZen.. thank you for the answer.. I was not at all clear with my question. the question I had in mind is this... I want to release an update for my app.I'm stuck on how to update the existing core data database which also stores data entered by the user. All i need to do is update a couple of records and preserve current user data. how do i do this??
See the topic of migration in Core Data Programming Guide. It sounds like you probably don't need anything other than the automatic migration functionality i.e. if you supply a new model, Core Data automatically upgrades the store. If you need something more complicated, the migration API can do just about anything.
@ TechZen... thank you for the reply. so if I use the migration API, it would automatically solve my issues right?
In simple cases, such as adding an attribute, automatic migration will usually work. However, there is an entire migration sub-API that lets you have control over the entire migration no matter how complex. The automatic migration handles most cases. The Apple Docs explain what it handles and what it does not.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.