Re: Programmatic Core Data Migration
Re: Programmatic Core Data Migration
- Subject: Re: Programmatic Core Data Migration
- From: tshanno <email@hidden>
- Date: Tue, 26 Feb 2013 09:59:00 -0600
Tom,
If you find a Core Data based app from somewhere else and look in the package contents you will find the .xcdatamodel file (or .xcdatamodeld if it has multiple versions). If you then look into a .xcdatamaodeld package you see every version of the data model that has shipped with that app.
Perhaps this will do a better job of demonstrating the need for all of the model versions to stick around:
YourApp 1.0 comes out with data model 1
Customers A & B buy your app
YourApp 2.0 comes out with data model 2
Customer A upgrades but B doesn't
YourApp 3.0 comes out with data model 3
Customers A & B both upgrade this time
If in the above example you only include the current data model (wether it is in the form of the Xcode data model editor file or your programmatically built one) customer A will have to re-enter all of her data as Core Data will not have a starting point with which to work out what changes it needs to make to here data store.
If you include the most recent data model and the previous one then customer A will be fine as she upgraded to version 2, then 3; customer B however will be screwed as Core Data will be missing the data model that was used to build his store and so have no idea what the structure was.
If at any point you need mapping models things get even worse. Say all three of those data models you made were different enough to need a mapping model. When you moved to version 2 you would make a mapping model from 1 to 2. When you moved to version 3 however you would need two more mapping models; one from version 2 to 3 as you would expect, and one from 1 to 3. This is because Core Data doesn't incrementally migrate the store, it makes the whole jump at once. With lightweight migrations as long as it has source and destination models it can work out the changes it needs even if there are 20 versions between.
I've never built a data model programmatically but I'm guessing it works like building a nib in code where it happens on each launch. This means that when you move to version 2 of your data model you will have to build model 1 and 2 on each launch. If this is still just for you to use at the moment you can drop older versions once you know you have moved all of you stores up to the newest version. The data model from any release versions will need to stick around forever (or at least a good long time).
Out of curiosity, what is the reasoning behind building the model in code rather than with the data model editor? The data editor is pretty stable from what I have seen and is super easy to use. If it is because of a visual impairment perhaps you could write your own app that allows you to specify the model's structure in text and converts it to a model file that can be saved as though Xcode had built it. That would eliminate the overhead of building the model at every launch and be easier to do with VoiceOver.
Let me know if you have any other questions.
Btw, data models aren't actually built in IB. Back in the days of Xcode 3 when IB was a separate app the data models were still edited in Xcode.
Mike Swan
www.theMikeSwan.com
On Feb 25, 2013, at 3:02 PM, tshanno <email@hidden> wrote:
> Hi, Michael.
>
> Thanks for the response. I can see that I must have a major lack of understanding when it comes to core data and how it works.
>
> So my assumption before your response was that the .xcdatamodeld file in Xcode was simply an IB interface for graphically designing the managed object model. Something to save you from actually having to code it. Since I didn't use it to create the object model and since the design was done entirely in code, my assumption was that this file was essentially a blank template.
>
> But, and please correct me if this isn't right, you seem to be implying that the model actually exists and is stored in the .xcdatamodeld file in between builds of my application despite the fact that I didn't actually use it directly in the design. Therefore, a new version of it must be created and the old version kept even though graphically when you click on both of them all you are going to see is a blank slate.
>
> Is this correct? Or do you mean something else by "keeping the old version of the model"?
>
> Thanks,
> Tom S.
>
> On Feb 25, 2013, at 12:17 PM, Michael Swan <email@hidden> wrote:
>
>> Tom,
>> The data store doesn't have a copy of the model in it, it just has hashes that are used to quickly compare the store to the data model trying to open it. In order for migration to work you will need to have both the old and new data models. In lightweight migrations Core Data uses the two versions to work out the differences between them and in the heavier styles it actually builds the old stack and carries the data over to the new stack a piece at a time.
>>
>> As you move forward with your application you will need to keep every data model in it from any version you release so that Core Data can get from any older version of the model to whatever is the current version. Depending on how often up change the model building all of those models each time your app launches may turn into a non-trivial amount of time down the road.
>>
>> Hope this helps,
>> Mike Swan
>> http://www.theMikeSwan.com
>>
>>
>>
>> "The Ego is the little self that pretends to be the only self and speaks so loudly that the still, small voice of the Greater Self, whose whisperings come from within the Soul, are rarely heard - unless we learn to listen."
>>
>>
>>
>> On Feb 25, 2013, at 8:36 AM, email@hidden wrote:
>>
>>> Message: 9
>>> Date: Mon, 25 Feb 2013 07:27:03 -0600
>>> From: tshanno <email@hidden>
>>> To: Cocoa Dev List <email@hidden>
>>> Subject: Programmatic Core Data Migration
>>> Message-ID: <email@hidden>
>>> Content-Type: text/plain; charset=us-ascii
>>>
>>> I'm at an early stage of developing my application and up to this point whenever I've made a changes to the core data model, I've simply trashed the old one and reimported default data. However I'm now at a point where I'd like to begin using the thing and, as a result, I want to keep the old data and use core data versioning and migration. I can't imagine that lightweight migration won't be fine for whatever changes I make from here on out.
>>>
>>> Many will think it foolish, I suppose, but I generated my object model completely programmatically. That is, I didn't use interface builder. Unfortunately, as far as I can tell, the information and instructions in the Core Data Model Versioning and Data Migration Programming Guide seem to assume that you did use interface builder and, therefore, suggest you create a new version of the model using Editor -> Add Model Version. I could do this but, of course, I'd simply have two blank graphs. :)
>>>
>>> So it isn't entirely clear to me what I should do here. Should I simply simply alter the version I have in my code, add the necessary options for lightweight migration? Xcode will therefore have the new version in code and the old version indirectly in the form of the old persistent store. Is that all it needs?
>>>
>>> Thanks for any help here.
>>>
>>> Tom S.
>>
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden